chunk.M6QEZOYU.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {
  2. l
  3. } from "./chunk.5MED2A3H.js";
  4. import {
  5. event,
  6. watch
  7. } from "./chunk.XX234VRK.js";
  8. import {
  9. e as e2
  10. } from "./chunk.YXKHB4AC.js";
  11. import {
  12. T,
  13. e,
  14. h,
  15. n,
  16. o,
  17. r,
  18. r2
  19. } from "./chunk.5PIDMFOE.js";
  20. import {
  21. __decorateClass
  22. } from "./chunk.IHGPZX35.js";
  23. // _uyihdawu1:/Users/claviska/Projects/shoelace/src/components/switch/switch.scss
  24. var switch_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 --height: var(--sl-toggle-size);\n --thumb-size: calc(var(--sl-toggle-size) + 4px);\n --width: calc(var(--height) * 2);\n display: inline-block;\n}\n\n.switch {\n display: inline-flex;\n align-items: center;\n font-family: var(--sl-input-font-family);\n font-size: var(--sl-input-font-size-medium);\n font-weight: var(--sl-input-font-weight);\n color: var(--sl-input-color);\n vertical-align: middle;\n cursor: pointer;\n}\n\n.switch__control {\n flex: 0 0 auto;\n position: relative;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: var(--width);\n height: var(--height);\n background-color: var(--sl-color-gray-300);\n border: solid var(--sl-input-border-width) var(--sl-color-gray-300);\n border-radius: var(--height);\n transition: var(--sl-transition-fast) border-color, var(--sl-transition-fast) background-color;\n}\n.switch__control .switch__thumb {\n width: var(--thumb-size);\n height: var(--thumb-size);\n background-color: var(--sl-color-white);\n border-radius: 50%;\n border: solid var(--sl-input-border-width) var(--sl-input-border-color);\n transform: translateX(calc(var(--width) / -2 + var(--thumb-size) / 2 - (var(--thumb-size) - var(--height)) / 2));\n transition: var(--sl-transition-fast) transform ease, var(--sl-transition-fast) background-color, var(--sl-transition-fast) border-color, var(--sl-transition-fast) box-shadow;\n}\n.switch__control input[type=checkbox] {\n position: absolute;\n opacity: 0;\n padding: 0;\n margin: 0;\n pointer-events: none;\n}\n\n.switch:not(.switch--checked):not(.switch--disabled) .switch__control:hover {\n background-color: var(--sl-color-gray-200);\n border-color: var(--sl-color-gray-200);\n}\n.switch:not(.switch--checked):not(.switch--disabled) .switch__control:hover .switch__thumb {\n background-color: var(--sl-color-white);\n border-color: var(--sl-input-border-color);\n}\n\n.switch.switch--focused:not(.switch--checked):not(.switch--disabled) .switch__control {\n background-color: var(--sl-color-gray-200);\n border-color: var(--sl-color-gray-200);\n}\n.switch.switch--focused:not(.switch--checked):not(.switch--disabled) .switch__control .switch__thumb {\n background-color: var(--sl-color-white);\n border-color: var(--sl-color-primary-500);\n box-shadow: 0 0 0 var(--sl-focus-ring-width) var(--sl-focus-ring-color-primary);\n}\n\n.switch--checked .switch__control {\n background-color: var(--sl-color-primary-500);\n border-color: var(--sl-color-primary-500);\n}\n.switch--checked .switch__control .switch__thumb {\n background-color: var(--sl-color-white);\n border-color: var(--sl-color-primary-500);\n transform: translateX(calc(var(--width) / 2 - var(--thumb-size) / 2 + (var(--thumb-size) - var(--height)) / 2));\n}\n\n.switch.switch--checked:not(.switch--disabled) .switch__control:hover {\n background-color: var(--sl-color-primary-400);\n border-color: var(--sl-color-primary-400);\n}\n.switch.switch--checked:not(.switch--disabled) .switch__control:hover .switch__thumb {\n background-color: var(--sl-color-white);\n border-color: var(--sl-color-primary-500);\n}\n\n.switch.switch--checked:not(.switch--disabled).switch--focused .switch__control {\n background-color: var(--sl-color-primary-400);\n border-color: var(--sl-color-primary-400);\n}\n.switch.switch--checked:not(.switch--disabled).switch--focused .switch__control .switch__thumb {\n background-color: var(--sl-color-white);\n border-color: var(--sl-color-primary-500);\n box-shadow: 0 0 0 var(--sl-focus-ring-width) var(--sl-focus-ring-color-primary);\n}\n\n.switch--disabled {\n opacity: 0.5;\n cursor: not-allowed;\n}\n\n.switch__label {\n line-height: var(--height);\n margin-left: 0.5em;\n user-select: none;\n}";
  25. // src/components/switch/switch.ts
  26. var id = 0;
  27. var SlSwitch = class extends h {
  28. constructor() {
  29. super(...arguments);
  30. this.switchId = `switch-${++id}`;
  31. this.labelId = `switch-label-${id}`;
  32. this.hasFocus = false;
  33. this.disabled = false;
  34. this.required = false;
  35. this.checked = false;
  36. this.invalid = false;
  37. }
  38. click() {
  39. this.input.click();
  40. }
  41. focus(options) {
  42. this.input.focus(options);
  43. }
  44. blur() {
  45. this.input.blur();
  46. }
  47. reportValidity() {
  48. return this.input.reportValidity();
  49. }
  50. setCustomValidity(message) {
  51. this.input.setCustomValidity(message);
  52. this.invalid = !this.input.checkValidity();
  53. }
  54. handleClick() {
  55. this.checked = !this.checked;
  56. }
  57. handleBlur() {
  58. this.hasFocus = false;
  59. this.slBlur.emit();
  60. }
  61. handleFocus() {
  62. this.hasFocus = true;
  63. this.slFocus.emit();
  64. }
  65. handleKeyDown(event2) {
  66. if (event2.key === "ArrowLeft") {
  67. event2.preventDefault();
  68. this.checked = false;
  69. }
  70. if (event2.key === "ArrowRight") {
  71. event2.preventDefault();
  72. this.checked = true;
  73. }
  74. }
  75. handleMouseDown(event2) {
  76. event2.preventDefault();
  77. this.input.focus();
  78. }
  79. handleCheckedChange() {
  80. if (this.input) {
  81. this.input.checked = this.checked;
  82. this.slChange.emit();
  83. }
  84. }
  85. render() {
  86. return T`
  87. <label
  88. part="base"
  89. for=${this.switchId}
  90. class=${e2({
  91. switch: true,
  92. "switch--checked": this.checked,
  93. "switch--disabled": this.disabled,
  94. "switch--focused": this.hasFocus
  95. })}
  96. @mousedown=${this.handleMouseDown}
  97. >
  98. <span part="control" class="switch__control">
  99. <span part="thumb" class="switch__thumb"></span>
  100. <input
  101. id=${this.switchId}
  102. type="checkbox"
  103. name=${l(this.name)}
  104. value=${l(this.value)}
  105. ?checked=${this.checked}
  106. ?disabled=${this.disabled}
  107. ?required=${this.required}
  108. role="switch"
  109. aria-checked=${this.checked ? "true" : "false"}
  110. aria-labelledby=${this.labelId}
  111. @click=${this.handleClick}
  112. @blur=${this.handleBlur}
  113. @focus=${this.handleFocus}
  114. @keydown=${this.handleKeyDown}
  115. />
  116. </span>
  117. <span part="label" id=${this.labelId} class="switch__label">
  118. <slot></slot>
  119. </span>
  120. </label>
  121. `;
  122. }
  123. };
  124. SlSwitch.styles = r(switch_default);
  125. __decorateClass([
  126. o('input[type="checkbox"]')
  127. ], SlSwitch.prototype, "input", 2);
  128. __decorateClass([
  129. r2()
  130. ], SlSwitch.prototype, "hasFocus", 2);
  131. __decorateClass([
  132. e()
  133. ], SlSwitch.prototype, "name", 2);
  134. __decorateClass([
  135. e()
  136. ], SlSwitch.prototype, "value", 2);
  137. __decorateClass([
  138. e({ type: Boolean, reflect: true })
  139. ], SlSwitch.prototype, "disabled", 2);
  140. __decorateClass([
  141. e({ type: Boolean, reflect: true })
  142. ], SlSwitch.prototype, "required", 2);
  143. __decorateClass([
  144. e({ type: Boolean, reflect: true })
  145. ], SlSwitch.prototype, "checked", 2);
  146. __decorateClass([
  147. e({ type: Boolean, reflect: true })
  148. ], SlSwitch.prototype, "invalid", 2);
  149. __decorateClass([
  150. event("sl-blur")
  151. ], SlSwitch.prototype, "slBlur", 2);
  152. __decorateClass([
  153. event("sl-change")
  154. ], SlSwitch.prototype, "slChange", 2);
  155. __decorateClass([
  156. event("sl-focus")
  157. ], SlSwitch.prototype, "slFocus", 2);
  158. __decorateClass([
  159. watch("checked")
  160. ], SlSwitch.prototype, "handleCheckedChange", 1);
  161. SlSwitch = __decorateClass([
  162. n("sl-switch")
  163. ], SlSwitch);
  164. var switch_default2 = SlSwitch;
  165. export {
  166. switch_default2 as switch_default
  167. };