Git Product home page Git Product logo

vue-stars's Introduction

vue-stars

Flexible Vue 3 input control for ratings (stars, etc.)

Demo

For a live demo, visit https://www.tallent.us/vue-stars/.

Status

This component is working and stable -- I don't have any future updates slated for it, but I'll update the build dependencies now and then, and I'm open to any bug fixes, etc.

Important: Version 2.0, released 2021-03-27, drops IE11 support and has been migrated to Vue 3 and Vite. Version 1 is still is available via npm if you need Vue 2 or IE11 support, but it will not be maintained going forward.

TypeScript Support

For some reason, it seems to be nearly impossible to get Vite to be configured to both emit .d.ts files on build and to allow use of npm run dev. The typescript2 plugin (recommended by some Vue experts) just seems to break HMR. So I've given up for now, until the Vite powers that be decide to make a functional template for libraries that builds typescript declarations. This has been the only real fly in the ointment for me migrating to Vite. If you have ideas, I'm all ears! In the meantime, I've decided I can no longer allow this issue to prevent me from releasing the new version.

Properties

The following properties are supported:

name

Name of the underlying form fields. The default is rating. This must be unique on your page, otherwise browsers will apply changes to one rating to others with the same name. This means if you have more than one <vue-stars> control on your page, this property is required.

readonly

Like native input controls, if this is set, the user cannot make changes to the value, but the control will still submit a value if it is part of a form. Hover animations are also disabled.

value

The integer value of the current rating, from 0 (no set value) to max. Since this is a number, be sure to use the v-bind syntax this attribute (e.g., :value="3" rather than value="3"). If not specified, the default value is 0.

max

The integer maximum rating (e.g., number of stars or other character the user can choose from). Since this is a number, be sure to use the v-bind syntax this attribute (e.g., :max="3" rather than max="3"). If not specified, the default is 5.

char

This is the character to use for each rating. The default is the Unicode star ().

If you would like to use a different character for each value from 1-max, you can provide a multi-character string. For example, a letter grade control could use :max="5" char="FDCBA", making the first rating value an F, the second a D, etc.

If max is longer than the string provided, the last character in char is used for all additional values. For example, :max="5" char="!★" would result in a rating control like this: !★★★★.

If you're using an icon font such as FontAwesome, providing a literal value could be troublesome since it won't display in your code editor. Also, Vue does not interpret HTML/XML entity references in attributes, so using something like char="&#xF005;" won't work. However, you can take advantage of v-bind's JavaScript interpretation and escape the character in Javascript, e.g., :char="'\uF005'".

While emoji characters are supported, many don't respond to CSS colors, so using a separate character for inactiveChar (below) is highly recommended. Keep in mind that if you use the JavaScript encoding for char, many emoji characters are outside the 16-bit range of \uXXXX, so you'll need to use the surrogate pair form (lead and tail). There's an example of this using smiling faces in the sample app.

inactiveChar

Sometimes, you may want to use a different set of characters for the "active" values than the "inactive" ones. This property works exactly like chars, but applies only to values between value+1 and max. If not provided, this falls back to the char property.

For example, to use the Unicode "white star" () for the inactive values, use inactive-char="☆".

This is especially useful for icon fonts such as FontAwesome that provide different glyphs for on/off state. It may also be a useful way to allow use of emoji characters, since those characters don't respond to CSS font color (making it difficult to distinguish the current value if the same character is used for active and inactive values).

Color Properties

Colors are configured using either the props below, or via CSS variables (in parenthesis).

activeColor (--active-color)

If specified, this overrides the default gold color used for the active values.

inactiveColor (--inactive-color)

If specified, this overrides the default grey color used for the active values.

hoverColor (--hover-color)

If specified, this overrides the default lighter gold color used when hovering over a value.

shadowColor (--shadow-color)

If specified, this overrides the default light yellow color used for the active values. (Inactive values don't have a shadow.)

Customizing the Style

Custom colors are handled using properties, as described above. Everything is sized to the relative font size, so sizing the component requires no special CSS, just set it's font-size in your CSS. To override all other styling, you can use plain old CSS. The main div for these components has a vue-stars class.

Events

Since this is a custom input control, this component emits a single event, input, when a new value is selected by the user (the value is returned as the first argument). This event is required for v-model to work properly (if you choose to use it).

Slots

While the built-in char and inactiveChar works for most use cases, you can used named slots instead to pass more complex markup for your "stars," such as svg or img tags, or multi-character strings.

activeLabel

Used this named slot to pass your own markup to render for an "active star." Because the content will be repeated for each active value, the slot-scope="props" attribute is required.

inactiveLabel

Used this named slot to pass your own markup to render for an "inactive star." Because the content will be repeated for each active value, the slot-scope="props" attribute is required.

Browser Compatibility

This component is at least compatible with the current versions of Chrome, Firefox, Edge, iOS Safari, and desktop Safari.

IE11 is no longer supported as of version 3.0.

Implementation Details

Under the hood, this control uses radio buttons. The buttons themselves are hidden, the user interacts with the corresponding <label> tags.

As with any Vue input component, the component's value property won't automatically change to match the user's selection (though the unerlying DOM value will). You'll need to either listen for the update:modelValue event and change the modelValue property yourself, or use v-model to set up two-way binding.

To work around a known (but obscure) issue with Apple iPhone/iPad, the hover animations are disabled for touch screen devices.

Build Setup

# install dependencies
npm install

# build for production with minification
npm run build

Import (required) and Global Registration (optional)

import { VueStars } from "vue-stars"
Vue.component("vue-stars", VueStars)

Release History

Date Version Notes
2017.10.28 0.1.0 First published version
2017.10.30 0.2.0 Fixes mostly for iOS Safari
2017.12.16 0.3.0 Rebuild configs from scratch, remove sample app, hopefully building a proper component now
2017.02.17 1.0.0 Rewrite CSS properties mechanism, add named/scoped slots
2018.12.23 1.0.1 Fix bleed-over of CSS into non-vue-rating checkbox input controls
2018.12.24 1.1.0 BROKEN. Change to using vue ui for development, inject CSS, update build process and deps.
2018.12.24 1.1.1 BROKEN. Fix module export issue.
2018.12.24 1.1.2 BROKEN. Still working on module export issues.
2018.12.24 1.1.3 Revolved build issue. Minor semi-breaking change, requires dereferenced import.
2019.05.26 1.2.0 Update deps. Adjusted CSS variables to allow external CSS control. Tweaked shadow.
2019.05.26 1.2.1 Buggered the build. Replaces 1.2.0
2020.05.24 1.2.2 Update deps. Fix for SSR (thanks @ulcuber!)
2021.03.27 2.0.0 Update to Vue 3, TS, Vite.

vue-stars's People

Contributors

richardtallent avatar ulcuber 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

vue-stars's Issues

Wrong values when using vue-stars multiple times in one page

When I use the component multiple times in one page as following :

<vue-stars :value="0" hover-color="#bf1e2e" active-color="#bf1e2e" shadow-color="#fff" readonly ></vue-stars>
<vue-stars :value="1" hover-color="#bf1e2e" active-color="#bf1e2e" shadow-color="#fff" readonly ></vue-stars>
<vue-stars :value="3" hover-color="#bf1e2e" active-color="#bf1e2e" shadow-color="#fff" readonly ></vue-stars>

I got this result (see the value):

image

So, how can I keep the value correct?

=================================

I am sorry, I did not read the documentation well

Solution

use unique name attribute for each one.

hoverLabel

Hello. Thank you for this great plugin.
Did you think about adding hover state slot?

Typescript support?

Hi Guys,

I would like to use your control but I am using typescript. Do you have support for this?

npm package

Hi,

It would be interesting to publish an npm package.

Regards,

NPM Broken

Well, I've "upgraded" to vue-cli, and now I can't figure out for the life of me how to build and publish this one little component to npm in a way that makes it importable from other projects.

I've been following the directions here, which mention nothing about what to do as the "main" entry in package.json, so I've been ticking up versions trying different approaches, with no luck.

https://vuejs.org/v2/cookbook/packaging-sfc-for-npm.html

Any help would be greatly appreciated. I'd like to get this version out to everyone (including me, I'm trying to use it in one of my own projects) without having to publish another 50 attempts.

Feature request: custom images instead of chars

I like idea and simplicity of this plugin, but I'm missing the option to use own images instead of just characters.
My goal is to use my custom images in a size I provide. I'll keep an eye on this repository. Thank you for your work so far!

Half stars

Is it possible to fill exact value? In example if the rating is "4.5" five stars are filled but I would need it to fill four and a half stars not to round it to five.

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.