chunk.YWOC6RDU.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {
  2. o as o2
  3. } from "./chunk.EPTJRW4G.js";
  4. import {
  5. clamp
  6. } from "./chunk.T56CG5BM.js";
  7. import {
  8. i
  9. } from "./chunk.DA3UDEH5.js";
  10. import {
  11. focusVisible
  12. } from "./chunk.XAZSQ3AT.js";
  13. import {
  14. event,
  15. watch
  16. } from "./chunk.XX234VRK.js";
  17. import {
  18. e as e2
  19. } from "./chunk.YXKHB4AC.js";
  20. import {
  21. T,
  22. e,
  23. h,
  24. n,
  25. o,
  26. r,
  27. r2
  28. } from "./chunk.5PIDMFOE.js";
  29. import {
  30. __decorateClass
  31. } from "./chunk.IHGPZX35.js";
  32. // _uyihdawu1:/Users/claviska/Projects/shoelace/src/components/rating/rating.scss
  33. var rating_default = ":host {\n position: relative;\n box-sizing: border-box;\n}\n:host *, :host *:before, :host *:after {\n box-sizing: inherit;\n}\n\n[hidden] {\n display: none !important;\n}\n\n:host {\n --symbol-color: var(--sl-color-gray-300);\n --symbol-color-active: #ffbe00;\n --symbol-size: 1.2rem;\n --symbol-spacing: var(--sl-spacing-xxx-small);\n display: inline-flex;\n}\n\n.rating {\n position: relative;\n display: inline-flex;\n border-radius: var(--sl-border-radius-medium);\n vertical-align: middle;\n}\n.rating:focus {\n outline: none;\n}\n.rating.focus-visible:focus {\n box-shadow: 0 0 0 var(--sl-focus-ring-width) var(--sl-focus-ring-color-primary);\n}\n\n.rating__symbols {\n display: inline-flex;\n position: relative;\n font-size: var(--symbol-size);\n line-height: 0;\n color: var(--symbol-color);\n white-space: nowrap;\n cursor: pointer;\n}\n.rating__symbols > * {\n padding: var(--symbol-spacing);\n}\n\n.rating__symbols--indicator {\n position: absolute;\n top: 0;\n left: 0;\n color: var(--symbol-color-active);\n pointer-events: none;\n}\n\n.rating__symbol {\n transition: var(--sl-transition-fast) transform;\n}\n\n.rating__symbol--hover {\n transform: scale(1.2);\n}\n\n.rating--disabled .rating__symbols,\n.rating--readonly .rating__symbols {\n cursor: default;\n}\n.rating--disabled .rating__symbol--hover,\n.rating--readonly .rating__symbol--hover {\n transform: none;\n}\n\n.rating--disabled {\n opacity: 0.5;\n}\n.rating--disabled .rating__symbols {\n cursor: not-allowed;\n}";
  34. // src/components/rating/rating.ts
  35. var SlRating = class extends h {
  36. constructor() {
  37. super(...arguments);
  38. this.hoverValue = 0;
  39. this.isHovering = false;
  40. this.value = 0;
  41. this.max = 5;
  42. this.precision = 1;
  43. this.readonly = false;
  44. this.disabled = false;
  45. this.getSymbol = (value) => '<sl-icon name="star-fill" library="system"></sl-icon>';
  46. }
  47. focus(options) {
  48. this.rating.focus(options);
  49. }
  50. blur() {
  51. this.rating.blur();
  52. }
  53. connectedCallback() {
  54. super.connectedCallback();
  55. this.updateComplete.then(() => focusVisible.observe(this.rating));
  56. }
  57. disconnectedCallback() {
  58. super.disconnectedCallback();
  59. focusVisible.unobserve(this.rating);
  60. }
  61. getValueFromMousePosition(event2) {
  62. return this.getValueFromXCoordinate(event2.clientX);
  63. }
  64. getValueFromTouchPosition(event2) {
  65. return this.getValueFromXCoordinate(event2.touches[0].clientX);
  66. }
  67. getValueFromXCoordinate(coordinate) {
  68. const containerLeft = this.rating.getBoundingClientRect().left;
  69. const containerWidth = this.rating.getBoundingClientRect().width;
  70. return clamp(this.roundToPrecision((coordinate - containerLeft) / containerWidth * this.max, this.precision), 0, this.max);
  71. }
  72. handleClick(event2) {
  73. this.setValue(this.getValueFromMousePosition(event2));
  74. }
  75. setValue(newValue) {
  76. if (this.disabled || this.readonly) {
  77. return;
  78. }
  79. this.value = newValue === this.value ? 0 : newValue;
  80. this.isHovering = false;
  81. }
  82. handleKeyDown(event2) {
  83. if (this.disabled || this.readonly) {
  84. return;
  85. }
  86. if (event2.key === "ArrowLeft") {
  87. const decrement = event2.shiftKey ? 1 : this.precision;
  88. this.value = Math.max(0, this.value - decrement);
  89. event2.preventDefault();
  90. }
  91. if (event2.key === "ArrowRight") {
  92. const increment = event2.shiftKey ? 1 : this.precision;
  93. this.value = Math.min(this.max, this.value + increment);
  94. event2.preventDefault();
  95. }
  96. if (event2.key === "Home") {
  97. this.value = 0;
  98. event2.preventDefault();
  99. }
  100. if (event2.key === "End") {
  101. this.value = this.max;
  102. event2.preventDefault();
  103. }
  104. }
  105. handleMouseEnter() {
  106. this.isHovering = true;
  107. }
  108. handleMouseMove(event2) {
  109. this.hoverValue = this.getValueFromMousePosition(event2);
  110. }
  111. handleMouseLeave() {
  112. this.isHovering = false;
  113. }
  114. handleTouchStart(event2) {
  115. this.hoverValue = this.getValueFromTouchPosition(event2);
  116. event2.preventDefault();
  117. }
  118. handleTouchMove(event2) {
  119. this.isHovering = true;
  120. this.hoverValue = this.getValueFromTouchPosition(event2);
  121. }
  122. handleTouchEnd(event2) {
  123. this.isHovering = false;
  124. this.setValue(this.hoverValue);
  125. event2.preventDefault();
  126. }
  127. handleValueChange() {
  128. this.slChange.emit();
  129. }
  130. roundToPrecision(numberToRound, precision = 0.5) {
  131. const multiplier = 1 / precision;
  132. return Math.ceil(numberToRound * multiplier) / multiplier;
  133. }
  134. render() {
  135. const counter = Array.from(Array(this.max).keys());
  136. let displayValue = 0;
  137. if (this.disabled || this.readonly) {
  138. displayValue = this.value;
  139. } else {
  140. displayValue = this.isHovering ? this.hoverValue : this.value;
  141. }
  142. return T`
  143. <div
  144. part="base"
  145. class=${e2({
  146. rating: true,
  147. "rating--readonly": this.readonly,
  148. "rating--disabled": this.disabled
  149. })}
  150. aria-disabled=${this.disabled ? "true" : "false"}
  151. aria-readonly=${this.readonly ? "true" : "false"}
  152. aria-value=${this.value}
  153. aria-valuemin=${0}
  154. aria-valuemax=${this.max}
  155. tabindex=${this.disabled ? "-1" : "0"}
  156. @click=${this.handleClick}
  157. @keydown=${this.handleKeyDown}
  158. @mouseenter=${this.handleMouseEnter}
  159. @touchstart=${this.handleTouchStart}
  160. @mouseleave=${this.handleMouseLeave}
  161. @touchend=${this.handleTouchEnd}
  162. @mousemove=${this.handleMouseMove}
  163. @touchmove=${this.handleTouchMove}
  164. >
  165. <span class="rating__symbols rating__symbols--inactive">
  166. ${counter.map((index) => {
  167. return T`
  168. <span
  169. class=${e2({
  170. rating__symbol: true,
  171. "rating__symbol--hover": this.isHovering && Math.ceil(displayValue) === index + 1
  172. })}
  173. role="presentation"
  174. @mouseenter=${this.handleMouseEnter.bind(this)}
  175. >
  176. ${o2(this.getSymbol(index + 1))}
  177. </span>
  178. `;
  179. })}
  180. </span>
  181. <span class="rating__symbols rating__symbols--indicator">
  182. ${counter.map((index) => {
  183. return T`
  184. <span
  185. class=${e2({
  186. rating__symbol: true,
  187. "rating__symbol--hover": this.isHovering && Math.ceil(displayValue) === index + 1
  188. })}
  189. style=${i({
  190. clipPath: displayValue > index + 1 ? "none" : `inset(0 ${100 - (displayValue - index) / 1 * 100}% 0 0)`
  191. })}
  192. role="presentation"
  193. >
  194. ${o2(this.getSymbol(index + 1))}
  195. </span>
  196. `;
  197. })}
  198. </span>
  199. </div>
  200. `;
  201. }
  202. };
  203. SlRating.styles = r(rating_default);
  204. __decorateClass([
  205. o(".rating")
  206. ], SlRating.prototype, "rating", 2);
  207. __decorateClass([
  208. r2()
  209. ], SlRating.prototype, "hoverValue", 2);
  210. __decorateClass([
  211. r2()
  212. ], SlRating.prototype, "isHovering", 2);
  213. __decorateClass([
  214. e({ type: Number })
  215. ], SlRating.prototype, "value", 2);
  216. __decorateClass([
  217. e({ type: Number })
  218. ], SlRating.prototype, "max", 2);
  219. __decorateClass([
  220. e({ type: Number })
  221. ], SlRating.prototype, "precision", 2);
  222. __decorateClass([
  223. e({ type: Boolean, reflect: true })
  224. ], SlRating.prototype, "readonly", 2);
  225. __decorateClass([
  226. e({ type: Boolean, reflect: true })
  227. ], SlRating.prototype, "disabled", 2);
  228. __decorateClass([
  229. e()
  230. ], SlRating.prototype, "getSymbol", 2);
  231. __decorateClass([
  232. event("sl-change")
  233. ], SlRating.prototype, "slChange", 2);
  234. __decorateClass([
  235. watch("value")
  236. ], SlRating.prototype, "handleValueChange", 1);
  237. SlRating = __decorateClass([
  238. n("sl-rating")
  239. ], SlRating);
  240. var rating_default2 = SlRating;
  241. export {
  242. rating_default2 as rating_default
  243. };