calcite-switch.entry.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-8ece2564.js';
  7. import { f as focusElement, t as toAriaBoolean } from './dom-da697a3f.js';
  8. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-50132b90.js';
  9. import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-bca481e1.js';
  10. import { u as updateHostInteraction } from './interactive-cb5bf285.js';
  11. import './guid-b4461004.js';
  12. const switchCss = "@-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([scale=s]) .container{height:0.75rem}:host([scale=s]) .track{height:0.75rem;width:1.5rem}:host([scale=s]) .handle{height:0.5rem;width:0.5rem}:host([scale=m]) .container{height:1rem}:host([scale=m]) .track{height:1rem;width:2rem}:host([scale=m]) .handle{height:0.75rem;width:0.75rem}:host([scale=l]) .container{height:1.5rem}:host([scale=l]) .track{height:1.5rem;width:3rem}:host([scale=l]) .handle{height:1.25rem;width:1.25rem}:host{position:relative;display:inline-block;width:auto;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;vertical-align:middle;tap-highlight-color:transparent}: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{width:auto;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}.track{pointer-events:none;position:relative;-webkit-box-sizing:border-box;box-sizing:border-box;display:inline-block;border-radius:9999px;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-2);background-color:var(--calcite-ui-foreground-2);vertical-align:top;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:150ms;transition-duration:150ms;-webkit-transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1)}.handle{pointer-events:none;position:absolute;display:block;border-radius:9999px;border-width:2px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:150ms;transition-duration:150ms;-webkit-transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);top:-1px;inset-inline:-1px auto}:host(:hover) .handle,:host(:focus) .handle{border-color:var(--calcite-ui-brand);-webkit-box-shadow:inset 0 0 0 1px var(--calcite-ui-brand);box-shadow:inset 0 0 0 1px var(--calcite-ui-brand)}:host([checked]) .track{border-color:var(--calcite-ui-brand-hover);background-color:var(--calcite-ui-brand)}:host([checked]) .handle{border-color:var(--calcite-ui-brand);inset-inline:auto -1px}:host([checked]:hover) .track{border-color:var(--calcite-ui-brand-hover);background-color:var(--calcite-ui-brand)}:host([checked]:hover) .handle{border-color:var(--calcite-ui-brand-hover);-webkit-box-shadow:inset 0 0 0 1px var(--calcite-ui-brand-hover);box-shadow:inset 0 0 0 1px var(--calcite-ui-brand-hover)}@media (forced-colors: active){:host([checked]) .track{background-color:canvasText}}.container:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}::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}";
  13. const Switch = class {
  14. constructor(hostRef) {
  15. registerInstance(this, hostRef);
  16. this.calciteSwitchChange = createEvent(this, "calciteSwitchChange", 7);
  17. //--------------------------------------------------------------------------
  18. //
  19. // Properties
  20. //
  21. //--------------------------------------------------------------------------
  22. /** True if the switch is disabled */
  23. this.disabled = false;
  24. /** The scale of the switch */
  25. this.scale = "m";
  26. /** True if the switch is initially on
  27. * @deprecated use 'checked' instead.
  28. */
  29. this.switched = false;
  30. /** True if the switch is initially on */
  31. this.checked = false;
  32. //--------------------------------------------------------------------------
  33. //
  34. // Private Methods
  35. //
  36. //--------------------------------------------------------------------------
  37. this.keyDownHandler = (e) => {
  38. const key = e.key;
  39. if (!this.disabled && (key === " " || key === "Enter")) {
  40. this.toggle();
  41. e.preventDefault();
  42. }
  43. };
  44. this.clickHandler = () => {
  45. this.toggle();
  46. };
  47. this.setSwitchEl = (el) => {
  48. this.switchEl = el;
  49. };
  50. }
  51. switchedWatcher(switched) {
  52. this.checked = switched;
  53. }
  54. //--------------------------------------------------------------------------
  55. //
  56. // Public Methods
  57. //
  58. //--------------------------------------------------------------------------
  59. /** Sets focus on the component. */
  60. async setFocus() {
  61. focusElement(this.switchEl);
  62. }
  63. onLabelClick() {
  64. if (!this.disabled) {
  65. this.toggle();
  66. this.setFocus();
  67. }
  68. }
  69. toggle() {
  70. this.checked = !this.checked;
  71. this.calciteSwitchChange.emit({
  72. // todo: We should remove emmitting redudant props in event payload.
  73. // https://github.com/Esri/calcite-components/issues/3163
  74. switched: this.checked
  75. });
  76. }
  77. //--------------------------------------------------------------------------
  78. //
  79. // Lifecycle
  80. //
  81. //--------------------------------------------------------------------------
  82. connectedCallback() {
  83. const initiallyChecked = this.checked || this.switched;
  84. if (initiallyChecked) {
  85. // if either prop is set, we ensure both are synced initially
  86. this.switched = this.checked = initiallyChecked;
  87. }
  88. connectLabel(this);
  89. connectForm(this);
  90. }
  91. disconnectedCallback() {
  92. disconnectLabel(this);
  93. disconnectForm(this);
  94. }
  95. componentDidRender() {
  96. updateHostInteraction(this);
  97. }
  98. // --------------------------------------------------------------------------
  99. //
  100. // Render Methods
  101. //
  102. // --------------------------------------------------------------------------
  103. render() {
  104. return (h(Host, { onClick: this.clickHandler, onKeyDown: this.keyDownHandler }, h("div", { "aria-checked": toAriaBoolean(this.checked), "aria-label": getLabelText(this), class: "container", ref: this.setSwitchEl, role: "switch", tabIndex: 0 }, h("div", { class: "track" }, h("div", { class: "handle" })), h(HiddenFormInputSlot, { component: this }))));
  105. }
  106. get el() { return getElement(this); }
  107. static get watchers() { return {
  108. "switched": ["switchedWatcher"]
  109. }; }
  110. };
  111. Switch.style = switchCss;
  112. export { Switch as calcite_switch };