calcite-rating.cjs.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-5c65e149.js');
  9. const guid = require('./guid-8b6d6cb4.js');
  10. const label = require('./label-165fc611.js');
  11. const form = require('./form-11926121.js');
  12. const interactive = require('./interactive-e294111f.js');
  13. require('./dom-9ac0341c.js');
  14. const TEXT = {
  15. rating: "Rating",
  16. stars: "Stars: ${num}"
  17. };
  18. 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}";
  19. const Rating = class {
  20. constructor(hostRef) {
  21. index.registerInstance(this, hostRef);
  22. this.calciteRatingChange = index.createEvent(this, "calciteRatingChange", 7);
  23. // --------------------------------------------------------------------------
  24. //
  25. // Properties
  26. //
  27. // --------------------------------------------------------------------------
  28. /** specify the scale of the component, defaults to m */
  29. this.scale = "m";
  30. /** the value of the rating component */
  31. this.value = 0;
  32. /** is the rating component in a selectable mode */
  33. this.readOnly = false;
  34. /** is the rating component in a selectable mode */
  35. this.disabled = false;
  36. /** Show average and count data summary chip (if available) */
  37. this.showChip = false;
  38. /** Localized string for "Rating" (used for aria label)
  39. * @default "Rating"
  40. */
  41. this.intlRating = TEXT.rating;
  42. /** Localized string for labelling each star, `${num}` in the string will be replaced by the number
  43. * @default "Stars: ${num}"
  44. */
  45. this.intlStars = TEXT.stars;
  46. /**
  47. * When true, makes the component required for form-submission.
  48. *
  49. * @internal
  50. */
  51. this.required = false;
  52. this.guid = `calcite-ratings-${guid.guid()}`;
  53. }
  54. //--------------------------------------------------------------------------
  55. //
  56. // Lifecycle
  57. //
  58. //--------------------------------------------------------------------------
  59. connectedCallback() {
  60. label.connectLabel(this);
  61. form.connectForm(this);
  62. }
  63. disconnectedCallback() {
  64. label.disconnectLabel(this);
  65. form.disconnectForm(this);
  66. }
  67. componentDidRender() {
  68. interactive.updateHostInteraction(this);
  69. }
  70. //--------------------------------------------------------------------------
  71. //
  72. // Event Listeners
  73. //
  74. //--------------------------------------------------------------------------
  75. blurHandler() {
  76. this.hasFocus = false;
  77. }
  78. // --------------------------------------------------------------------------
  79. //
  80. // Render Methods
  81. //
  82. // --------------------------------------------------------------------------
  83. renderStars() {
  84. return [1, 2, 3, 4, 5].map((i) => {
  85. const selected = this.value >= i;
  86. const average = this.average && !this.value && i <= this.average;
  87. const hovered = i <= this.hoverValue;
  88. const fraction = this.average && this.average + 1 - i;
  89. const partial = !this.value && !hovered && fraction > 0 && fraction < 1;
  90. const focused = this.hasFocus && this.focusValue === i;
  91. return (index.h("span", { class: { wrapper: true } }, index.h("label", { class: { star: true, focused, selected, average, hovered, partial }, htmlFor: `${this.guid}-${i}`, onMouseOver: () => {
  92. this.hoverValue = i;
  93. } }, index.h("calcite-icon", { "aria-hidden": "true", class: "icon", icon: selected || average || this.readOnly ? "star-f" : "star", scale: this.scale }), partial && (index.h("div", { class: "fraction", style: { width: `${fraction * 100}%` } }, index.h("calcite-icon", { icon: "star-f", scale: this.scale }))), index.h("span", { class: "visually-hidden" }, this.intlStars.replace("${num}", `${i}`))), index.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) =>
  94. // click is fired from the the component's label, so we treat this as an internal event
  95. event.stopPropagation(), onFocus: () => {
  96. this.hasFocus = true;
  97. this.focusValue = i;
  98. }, ref: (el) => (i === 1 || i === this.value) && (this.inputFocusRef = el), type: "radio", value: i })));
  99. });
  100. }
  101. render() {
  102. const { disabled, intlRating, showChip, scale, count, average } = this;
  103. return (index.h(index.Fragment, null, index.h("fieldset", { class: "fieldset", disabled: disabled, onBlur: () => (this.hoverValue = null), onMouseLeave: () => (this.hoverValue = null), onTouchEnd: () => (this.hoverValue = null) }, index.h("legend", { class: "visually-hidden" }, intlRating), this.renderStars()), (count || average) && showChip ? (index.h("calcite-chip", { scale: scale, value: count === null || count === void 0 ? void 0 : count.toString() }, !!average && index.h("span", { class: "number--average" }, average.toString()), !!count && index.h("span", { class: "number--count" }, "(", count === null || count === void 0 ? void 0 :
  104. count.toString(), ")"))) : null, index.h(form.HiddenFormInputSlot, { component: this })));
  105. }
  106. //--------------------------------------------------------------------------
  107. //
  108. // Private Methods
  109. //
  110. //--------------------------------------------------------------------------
  111. onLabelClick() {
  112. this.setFocus();
  113. }
  114. updateValue(value) {
  115. this.value = value;
  116. this.calciteRatingChange.emit({ value });
  117. }
  118. //--------------------------------------------------------------------------
  119. //
  120. // Public Methods
  121. //
  122. //--------------------------------------------------------------------------
  123. /** Sets focus on the component. */
  124. async setFocus() {
  125. this.inputFocusRef.focus();
  126. }
  127. get el() { return index.getElement(this); }
  128. };
  129. Rating.style = ratingCss;
  130. exports.calcite_rating = Rating;