calcite-rating.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Fragment } from '@stencil/core/internal/client';
  7. import { g as guid } from './guid.js';
  8. import { c as connectLabel, d as disconnectLabel } from './label.js';
  9. import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { d as defineCustomElement$3 } from './chip.js';
  12. import { d as defineCustomElement$2 } from './icon.js';
  13. const TEXT = {
  14. rating: "Rating",
  15. stars: "Stars: ${num}"
  16. };
  17. const ratingCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;width:-moz-fit-content;width:-webkit-fit-content;width:fit-content}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([scale=s]){height:1.5rem;--calcite-rating-spacing-unit:0.25rem}:host([scale=m]){height:2rem;--calcite-rating-spacing-unit:0.5rem}:host([scale=l]){height:2.75rem;--calcite-rating-spacing-unit:0.75rem}:host([read-only]){pointer-events:none}.fieldset{margin:0px;display:-ms-flexbox;display:flex;border-width:0px;padding:0px}.wrapper{display:inline-block;-webkit-margin-end:var(--calcite-rating-spacing-unit);margin-inline-end:var(--calcite-rating-spacing-unit)}.star{position:relative;display:-ms-flexbox;display:flex;cursor:pointer;outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;color:var(--calcite-ui-border-input);-webkit-transition:var(--calcite-animation-timing);transition:var(--calcite-animation-timing);-webkit-transform:scale(1);transform:scale(1)}.star:active{-webkit-transform:scale(1.1);transform:scale(1.1)}.focused{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}.average,.fraction{color:var(--calcite-ui-warning)}.hovered,.selected,:host([read-only]) .average,:host([read-only]) .fraction{color:var(--calcite-ui-brand)}.hovered:not(.selected){-webkit-transform:scale(0.9);transform:scale(0.9)}:host .fraction{pointer-events:none;position:absolute;top:0px;overflow:hidden;-webkit-transition:var(--calcite-animation-timing);transition:var(--calcite-animation-timing);inset-inline-start:0}calcite-chip{pointer-events:none;cursor:default}.number--average{font-weight:var(--calcite-font-weight-bold)}.number--count{color:var(--calcite-ui-text-2);font-style:italic}.number--count:not(:first-child){-webkit-margin-start:var(--calcite-rating-spacing-unit);margin-inline-start:var(--calcite-rating-spacing-unit)}.visually-hidden{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  18. const Rating = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  19. constructor() {
  20. super();
  21. this.__registerHost();
  22. this.__attachShadow();
  23. this.calciteRatingChange = createEvent(this, "calciteRatingChange", 7);
  24. // --------------------------------------------------------------------------
  25. //
  26. // Properties
  27. //
  28. // --------------------------------------------------------------------------
  29. /** specify the scale of the component, defaults to m */
  30. this.scale = "m";
  31. /** the value of the rating component */
  32. this.value = 0;
  33. /** is the rating component in a selectable mode */
  34. this.readOnly = false;
  35. /** is the rating component in a selectable mode */
  36. this.disabled = false;
  37. /** Show average and count data summary chip (if available) */
  38. this.showChip = false;
  39. /** Localized string for "Rating" (used for aria label)
  40. * @default "Rating"
  41. */
  42. this.intlRating = TEXT.rating;
  43. /** Localized string for labelling each star, `${num}` in the string will be replaced by the number
  44. * @default "Stars: ${num}"
  45. */
  46. this.intlStars = TEXT.stars;
  47. /**
  48. * When true, makes the component required for form-submission.
  49. *
  50. * @internal
  51. */
  52. this.required = false;
  53. this.guid = `calcite-ratings-${guid()}`;
  54. }
  55. //--------------------------------------------------------------------------
  56. //
  57. // Lifecycle
  58. //
  59. //--------------------------------------------------------------------------
  60. connectedCallback() {
  61. connectLabel(this);
  62. connectForm(this);
  63. }
  64. disconnectedCallback() {
  65. disconnectLabel(this);
  66. disconnectForm(this);
  67. }
  68. componentDidRender() {
  69. updateHostInteraction(this);
  70. }
  71. //--------------------------------------------------------------------------
  72. //
  73. // Event Listeners
  74. //
  75. //--------------------------------------------------------------------------
  76. blurHandler() {
  77. this.hasFocus = false;
  78. }
  79. // --------------------------------------------------------------------------
  80. //
  81. // Render Methods
  82. //
  83. // --------------------------------------------------------------------------
  84. renderStars() {
  85. return [1, 2, 3, 4, 5].map((i) => {
  86. const selected = this.value >= i;
  87. const average = this.average && !this.value && i <= this.average;
  88. const hovered = i <= this.hoverValue;
  89. const fraction = this.average && this.average + 1 - i;
  90. const partial = !this.value && !hovered && fraction > 0 && fraction < 1;
  91. const focused = this.hasFocus && this.focusValue === i;
  92. return (h("span", { class: { wrapper: true } }, h("label", { class: { star: true, focused, selected, average, hovered, partial }, htmlFor: `${this.guid}-${i}`, onMouseOver: () => {
  93. this.hoverValue = i;
  94. } }, h("calcite-icon", { "aria-hidden": "true", class: "icon", icon: selected || average || this.readOnly ? "star-f" : "star", scale: this.scale }), partial && (h("div", { class: "fraction", style: { width: `${fraction * 100}%` } }, h("calcite-icon", { icon: "star-f", scale: this.scale }))), h("span", { class: "visually-hidden" }, this.intlStars.replace("${num}", `${i}`))), h("input", { checked: i === this.value, class: "visually-hidden", disabled: this.disabled || this.readOnly, id: `${this.guid}-${i}`, name: this.guid, onChange: () => this.updateValue(i), onClick: (event) =>
  95. // click is fired from the the component's label, so we treat this as an internal event
  96. event.stopPropagation(), onFocus: () => {
  97. this.hasFocus = true;
  98. this.focusValue = i;
  99. }, ref: (el) => (i === 1 || i === this.value) && (this.inputFocusRef = el), type: "radio", value: i })));
  100. });
  101. }
  102. render() {
  103. const { disabled, intlRating, showChip, scale, count, average } = this;
  104. return (h(Fragment, null, h("fieldset", { class: "fieldset", disabled: disabled, onBlur: () => (this.hoverValue = null), onMouseLeave: () => (this.hoverValue = null), onTouchEnd: () => (this.hoverValue = null) }, h("legend", { class: "visually-hidden" }, intlRating), this.renderStars()), (count || average) && showChip ? (h("calcite-chip", { scale: scale, value: count === null || count === void 0 ? void 0 : count.toString() }, !!average && h("span", { class: "number--average" }, average.toString()), !!count && h("span", { class: "number--count" }, "(", count === null || count === void 0 ? void 0 :
  105. count.toString(), ")"))) : null, h(HiddenFormInputSlot, { component: this })));
  106. }
  107. //--------------------------------------------------------------------------
  108. //
  109. // Private Methods
  110. //
  111. //--------------------------------------------------------------------------
  112. onLabelClick() {
  113. this.setFocus();
  114. }
  115. updateValue(value) {
  116. this.value = value;
  117. this.calciteRatingChange.emit({ value });
  118. }
  119. //--------------------------------------------------------------------------
  120. //
  121. // Public Methods
  122. //
  123. //--------------------------------------------------------------------------
  124. /** Sets focus on the component. */
  125. async setFocus() {
  126. this.inputFocusRef.focus();
  127. }
  128. get el() { return this; }
  129. static get style() { return ratingCss; }
  130. }, [1, "calcite-rating", {
  131. "scale": [513],
  132. "value": [1538],
  133. "readOnly": [516, "read-only"],
  134. "disabled": [516],
  135. "showChip": [516, "show-chip"],
  136. "count": [514],
  137. "average": [514],
  138. "name": [513],
  139. "intlRating": [1, "intl-rating"],
  140. "intlStars": [1, "intl-stars"],
  141. "required": [516],
  142. "hoverValue": [32],
  143. "focusValue": [32],
  144. "hasFocus": [32],
  145. "setFocus": [64]
  146. }, [[0, "blur", "blurHandler"]]]);
  147. function defineCustomElement$1() {
  148. if (typeof customElements === "undefined") {
  149. return;
  150. }
  151. const components = ["calcite-rating", "calcite-chip", "calcite-icon"];
  152. components.forEach(tagName => { switch (tagName) {
  153. case "calcite-rating":
  154. if (!customElements.get(tagName)) {
  155. customElements.define(tagName, Rating);
  156. }
  157. break;
  158. case "calcite-chip":
  159. if (!customElements.get(tagName)) {
  160. defineCustomElement$3();
  161. }
  162. break;
  163. case "calcite-icon":
  164. if (!customElements.get(tagName)) {
  165. defineCustomElement$2();
  166. }
  167. break;
  168. } });
  169. }
  170. defineCustomElement$1();
  171. const CalciteRating = Rating;
  172. const defineCustomElement = defineCustomElement$1;
  173. export { CalciteRating, defineCustomElement };