Coder Social home page Coder Social logo

starability's Introduction

Starability.css - Accessible rating with animations on top

Accessible rating forms with cute animations on top. The name is the combination of two words: star and accessibility (or ability in general, whatever pleases you). Starability ratings are made with HTML, CSS (no JavaScript), and are accessible by keyboard, so even people using screen readers are able to use them. To achieve this effect I am using the sibling combinators technique that was introduced by Lea Verou in "Accessible star rating widget with pure CSS" blogpost, but it is bit upgraded: does not rely on a reverse order of the inputs, what makes it even more accessible. The goal of this small library was to combine the Lea's technique with neat animations and make it easy to quickly use it on your website.

Remember: to submit the data you will need to add a bit of your own code. Here we have just a fieldset with a collection of radio buttons, neatly styled. There are no dependencies in this library.

Important: Since version 2.0 the library does not rely on a reverse order of inputs. This means that the issue with the keyboard arrows moving in a reverse direction is no longer there. It is strongly advised to update the HTML structure in your project, although the updated CSS doesn't break the functionality even for the previous version of HTML markup.

Demo

You can check how Starability.css looks here.

Folder Structure

This step by step tutorial is based on an assumption that project directory structure looks as follows:

  • index.html
  • /css
    • css file with chosen rating (eg.: starability-fade.min.css)

If you have a different file structure, remember to update file path for CSS stylesheet links (in HTML <head>).

How to use

  1. Copy the structure of HTML (fieldset and insides), to make sure the styles work correctly. If you want more than one rating widget on the same page, remember that the id's need to be different for each radio buttons collection:

    <fieldset class="starability-basic">
      <legend>First rating:</legend>
      <input type="radio" id="no-rate" class="input-no-rate" name="rating" value="0" checked aria-label="No rating." />
      <input type="radio" id="first-rate1" name="rating" value="1" />
      <label for="first-rate1" title="Terrible">1 star</label>
      <input type="radio" id="first-rate2" name="rating" value="2" />
      <label for="first-rate2" title="Not good">2 stars</label>
      <input type="radio" id="first-rate3" name="rating" value="3" />
      <label for="first-rate3" title="Average">3 stars</label>
      <input type="radio" id="first-rate4" name="rating" value="4" />
      <label for="first-rate4" title="Very good">4 stars</label>
      <input type="radio" id="first-rate5" name="rating" value="5" />
      <label for="first-rate5" title="Amazing">5 stars</label>
    </fieldset>
    
    <fieldset class="starability-basic">
      <legend>First rating:</legend>
      <input type="radio" id="no-rate" class="input-no-rate" name="rating" value="0" checked aria-label="No rating." />
      <input type="radio" id="second-rate1" name="rating" value="1" />
      <label for="second-rate1" title="Terrible">1 star</label>
      <input type="radio" id="second-rate2" name="rating" value="2" />
      <label for="second-rate2" title="Not good">2 stars</label>
      <input type="radio" id="second-rate3" name="rating" value="3" />
      <label for="second-rate3" title="Average">3 stars</label>
      <input type="radio" id="second-rate4" name="rating" value="4" />
      <label for="second-rate4" title="Very good">4 stars</label>
      <input type="radio" id="second-rate5" name="rating" value="5" />
      <label for="second-rate5" title="Amazing">5 stars</label>
    </fieldset>
  2. Add a stylesheet with a name of an animation that works best for your app name inside your website's <head> element. There are the minified versions or standard CSS files. Let's say we want a fading animation:

    <head>
      <link rel="stylesheet" type="text/css" href="css/starability-fade.min.css"/>
    </head>

    If you wish to use more than one animation per website (though better stick to one!), you can include the file with all of the animations combined:

    <head>
      <link rel="stylesheet" type="text/css" href="css/starability-all.min.css"/>
    </head>
  3. Add the chosen rating version class name to the fieldset element in your HTML. For our fading animation you would have to add the .starability-fade class:

    <fieldset class="starability-fade">

List of rating animations

For now you can choose one of the animations listed below. Go to the demo to see how they look.

  • starability-basic
  • starability-slot
  • starability-grow
  • starability-growRotate
  • starability-fade
  • starability-checkmark
  • starability-heart
  • starability-heartbeat
  • starability-coinFlip (by Paulina Materna)

Supported browsers

Starability.css works in IE10+ and all modern browsers. If you want to support browsers below IE10, you should disable animations for them, for example by using Modernizr.

Disabling hover action

This rating system by default reacts to :hover, changing the background image position. This is an operation that causes repaints. If you want to make your website as light as possible, you can remove declaration that will highlight stars on hover:

.starability-fade > input:hover ~ label {
  background-position: 0 -30px;
}

Disabling high contrast mode support

This rating system by default supports high contrast mode for the Windows users. This means that if user is using high contrast mode, there is no star styling and the regular fieldset with checkboxes is displayed. It is not advised to turn this mode off, but it is possible to do that by setting the $high-contrast-enabled variable to false in _variables.scss file

Accessible outline

By default, if user focuses with keyboard on the rating widget, there is an outline around all the stars to make it easier to navigate. This is not visible for users using only mouse. The function can be turned off by setting the $accessible-focus-ring variable to false in _variables.scss file and removing a span with .starability-focus-ring class from the HTML. It is not advised though to remove the outline.

There is also another functionality that can be turned on by changing the $accessible-outline variable value to true. It will show an outline around active stars that are in the keyboard focus. It can enhance the widget's accessibility for some users.

Showing the static rating result

You can add the static results of the rating anywhere you need it. To indicate how many stars were added, change the data-rating value on an element with the starability-result class. Please note, that this supports only integer numbers. If you wish to show the results as the float numbers, you might need to use other solution.

  <h3>Rated element name</h3>
  <p class="starability-result" data-rating="3">
    Rated: 3 stars
  </p>

There is no official support for half-stars, as we don't want to make the library output bigger than needed, but you can easily add the support for showing half-stars in the rating results. To do so, check the answer in this issue.

Further customisation with SASS

If you are well-versed with SASS, you can easily adjust rating widget to your needs, e.g. have a 10-star based system or turn off outline and hover. It can be done by setting true/false values to the variables in the _variables.scss file and running the gulp task to process the files. Of course, to have a 10-stars system you also need add additional radio inputs in your HTML.

If you are not using SCSS syntax, you can also have a 10-star based system. Just remember to change the width of the fieldset element in your CSS, so that the stars would all fit it (for example for 10 stars, the width of fieldset should be 10x the width of a star, so 300px).

Changelog

Check what was changed in the newest version in the Changelog.

Contributors

The library wouldn't be what it is now without:

License

The library is licensed under The MIT License (MIT).

Go to the Lunar Logic website to learn more about us and our work.

starability's People

Contributors

arturparkhisenko avatar barryels avatar dandv avatar mislav avatar paulinamaterna avatar scottaohara avatar stevelacey avatar szynszyliszys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

starability's Issues

Touch gesture support

Great work! A pan-gesture support for touchscreens would it make even more awesome ;)

Incorrect reference for variables in starability-base

Hey there,

Noticed in _starability-base.scss that the import for the variables is:
@import '../variables';

but that throws an error when trying to compile, because variables and starability-base are actually in the same folder as each other.

figured it'd be kinda lame to make a PR simply to change the above to:
@import 'variables';

:) thanks!

Populating with data

Hi,

Really not sure if this is a 'code' issue (well actually more a usability issue), but I wanted to see how you would set that star rating from a variable?

So, if I have a variable 'newRating' with the value of 4 how would I set your Starability to reflect this, instead of clicking on the 4th star?

fixing required in starability-heartbeat.css

line! -> 173-174
display: inline-block;
float: left;

problem?--inline-block is ignored due to the float. If 'float' has a value other than 'none', the box is floated and 'display' is treated as 'block'

->so this should fix the issue...

display: inline-block;
float: left;

Rating starts with yellow stars instead of grey stars

When I use the newest version of starability, the stars appear yellow (as if a 5-star rating already had been given), although NONE of the radio buttons has is "checked".

This is misleading the user. The rating should be shown in grey initially.

The solution in #21 does not do the trick entirely as it breaks the proper coloring of the stars after an initial rating has been set, for example: Check 5 stars, then check one star. While hovering over the first star, the other stars stay yellow.

Multiple ratings on same page

Is it possible to display multiple ratings on the same page.

I have the need for four individual items to be rated per review but when attempting to use more than one the ratings for others only interact with and manipulate the original. Even when I change the input name to a unique name for each rating.

Sub-star ratings

Hello. Over at DSC, we use our own setup for star ratings.

I applaud you for devising an accessible solution to this issue. Is sub-star rating a possiblity? i.e. 4.7/5? We've come up with some crafty solutions but I fear they are not as accessible as this.

Node throws SyntaxError: Unexpected token '.' for npm package css

I've installed starability and required it in my app.js file, and when I try to run the app, it breaks with this syntax error:

node_modules\starability\starability-css\starability-all.css:1
.starability-result {
^

SyntaxError: Unexpected token '.'

Dunno if this is specific to this package, or if Node itself is reading it wrong. I've looked at the file and it looks fine to me.

When I remove the require statement for starability, the app starts just fine.

Better support for `disabled` attribute.

Value of disabled input cannot be changed, that is good. Yet the stars on hover have the same color as when it's active. Hovering disabled input shouldn't change color to yellow. This can be easily achieved:

  @if ($hover-enabled) {
    > input:hover:not(:disabled) ~ label {
      background-position: 0 (-$star-size);
    }
  }

edit

Another thing - the coursor shouldn't be set to pointer in such case.

  > input:disabled+label {
    cursor: not-allowed;
    // or default
  }

Responsive Star Size

Stars are not responsive according to the screen size.
How to make them responsive?

Static ratings

A typical use-case (or at least my use-case) is not only to collect these ratings but also render them back to the user. I currently do this with font-awesome icons because they look like your stars, they're scalable, I am lazy, and you've used a sprite for the images – an (accessible) example/support for this would be handy for others I am sure.

How to remove rate selecion

I noticed that after clicking on a star, there's no way for the user to undo a selection. I was wondering how one could achieve this goal to allow users to regret rating at all.

Accessibility

Screen reader does not read the label title which is highly informative. Consider adding aria-label="[title text] + [label text]". For example aria-label="Average 3 stars". FYI, aria-label overrides the label text itself so screen reader will only hear this once.

Stars invisible in high contrast mode

Hi again.

I was checking starability, since it's been awhile, and noticed that if a user has high contrast mode enabled, that all of the stars vanish, due to them being background images.

Thinking of solutions for this, I've gotten around this issue by using SVGs in place of background images, though I realize that'd be quite a bit of work, and could make some of the animations difficult.

That said, being in high contrast mode, design isn't nearly as critical, as much as it's about having a usable interface. With that in mind, an alternative could be to have a high contrast mode media query where the UI changed from ☆ &star; vs ★ &#9733; to indicated a selected vs non-selected rating.

While typically I wouldn't want to use those sorts of characters in CSS content, to mitigate the possibility of screen readers reading out "black star" and such, I'd consider it a minor annoyance for the individuals who are using both a screen reader & high contrast mode. And it would far outweigh the annoyance of having invisible UI.

Let me know if you have any questions about this that I didn't cover.

Thanks :)

Invisible input creates accessibility issue

In trying to solve issues 21 and 22, an "invisible" input was added to allow for a no-rating option. However, for sighted keyboard users, this creates an issue as there is no indicator that this element has been selected.

For example:
Load up a demo with the markup currently outlined in the readme.

Attempt to tab through the interface with keyboard alone and you will find that there is no focus indicator to give guidance to sighted keyboard users.

I do not have a firm idea of a proper solution that might fit exactly with the design aesthetic you'd like to maintain for this project, but here is one thought I might entertain if I had the time to spend on it myself:

Reveal a text label indicating that there is no current rating when a user has focused on the no-rating input. This text label could be revealed above the stars, like one of those floating label patterns. Regardless, it should be noticeable so that a user can easily see a visible change when focus is on that element, or not.

I hope this provides you with at least an idea of how to fix this accessibility issue with the rating component.

Please let me know if there is anything I could further clarify.

Change size of stars?

Is there a way to change the size of the stars? I would like to make them bigger and changing height and width properties in the HTML has no effect.

Focus ring is not demonstrated in example HTML

The README states “It is not advised though to remove the outline.” However, the .starability-focus-ring element isn't shown in the example HTML for the rating widget.

Furthermore, the focus ring CSS depends on an element with ID #no-rate. If there are multiple rating widgets on the page, they would all need to share the same element ID for this to work. This might not be a huge problem, but it might trigger duplicate ID detection mechanisms in some apps. Consider switching to a class name instead.

Star size

What's the best way to change the star size? I've changed the $star-size in _variables.scss but i get an unexpected output, I'm guessing that it's because the star base64 image has set dimensions and to display the star it's actually using the image as a background image?

How should I go about resizing the stars? Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.