calcite-block-section.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.97
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client/index.js';
  7. import { c as getElementDir, t as toAriaBoolean } from './dom.js';
  8. import { g as guid } from './guid.js';
  9. import { i as isActivationKey } from './key.js';
  10. import { d as defineCustomElement$3 } from './icon.js';
  11. import { d as defineCustomElement$2 } from './switch.js';
  12. const CSS = {
  13. content: "content",
  14. invalid: "invalid",
  15. toggle: "toggle",
  16. toggleSwitch: "toggle--switch",
  17. toggleSwitchContent: "toggle--switch__content",
  18. toggleSwitchText: "toggle--switch__text",
  19. sectionHeader: "section-header",
  20. sectionHeaderText: "section-header__text",
  21. statusIcon: "status-icon",
  22. valid: "valid"
  23. };
  24. const TEXT = {
  25. collapse: "Collapse",
  26. expand: "Expand"
  27. };
  28. const ICONS = {
  29. menuOpen: "chevron-down",
  30. menuClosedLeft: "chevron-left",
  31. menuClosedRight: "chevron-right",
  32. valid: "check-circle",
  33. invalid: "exclamation-mark-triangle"
  34. };
  35. const blockSectionCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity: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;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{box-sizing:border-box;display:block;background-color:var(--calcite-ui-foreground-1);font-size:var(--calcite-font-size--1);color:var(--calcite-ui-text-2)}:host([open]){border-width:0px;border-block-end-width:1px;border-style:solid;border-block-end-color:var(--calcite-ui-border-3)}:host(:last-child){border-block-end-width:0px}.toggle{inline-size:100%;border-width:0px;background-color:transparent;font-family:var(--calcite-sans-family);font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-2)}.toggle--switch,.section-header{margin-inline:0px;margin-block:0.25rem;display:flex;cursor:pointer;-webkit-user-select:none;user-select:none;align-items:center;padding-inline:0px;padding-block:0.5rem;font-size:var(--calcite-font-size--1);outline-color:transparent}.toggle--switch:focus,.section-header:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}.toggle--switch:hover,.section-header:hover{color:var(--calcite-ui-text-1)}.section-header .status-icon{align-self:flex-end}.section-header__text{margin-inline:0.75rem;margin-block:0px;flex:1 1 auto;text-align:initial;word-wrap:anywhere}.toggle--switch calcite-switch{pointer-events:none;margin-inline-start:0.25rem}.toggle--switch .status-icon{margin-inline-start:0.5rem}.toggle--switch__content{display:flex;flex:1 1 auto;align-items:center}.status-icon.valid{color:var(--calcite-ui-success)}.status-icon.invalid{color:var(--calcite-ui-danger)}";
  36. const BlockSection = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  37. constructor() {
  38. super();
  39. this.__registerHost();
  40. this.__attachShadow();
  41. this.calciteBlockSectionToggle = createEvent(this, "calciteBlockSectionToggle", 6);
  42. /**
  43. * When `true`, expands the component and its contents.
  44. */
  45. this.open = false;
  46. /**
  47. * Specifies the component's toggle display -
  48. *
  49. * `"button"` (selectable header), or
  50. *
  51. * `"switch"` (toggle switch).
  52. */
  53. this.toggleDisplay = "button";
  54. this.guid = guid();
  55. // --------------------------------------------------------------------------
  56. //
  57. // Private Methods
  58. //
  59. // --------------------------------------------------------------------------
  60. this.handleHeaderKeyDown = (event) => {
  61. if (isActivationKey(event.key)) {
  62. this.toggleSection();
  63. event.preventDefault();
  64. event.stopPropagation();
  65. }
  66. };
  67. this.toggleSection = () => {
  68. this.open = !this.open;
  69. this.calciteBlockSectionToggle.emit();
  70. };
  71. }
  72. // --------------------------------------------------------------------------
  73. //
  74. // Render Methods
  75. //
  76. // --------------------------------------------------------------------------
  77. renderStatusIcon() {
  78. var _a;
  79. const { status } = this;
  80. const statusIcon = (_a = ICONS[status]) !== null && _a !== void 0 ? _a : false;
  81. const statusIconClasses = {
  82. [CSS.statusIcon]: true,
  83. [CSS.valid]: status == "valid",
  84. [CSS.invalid]: status == "invalid"
  85. };
  86. return !!statusIcon ? (h("calcite-icon", { class: statusIconClasses, icon: statusIcon, scale: "s" })) : null;
  87. }
  88. render() {
  89. const { el, intlCollapse, intlExpand, open, text, toggleDisplay } = this;
  90. const dir = getElementDir(el);
  91. const arrowIcon = open
  92. ? ICONS.menuOpen
  93. : dir === "rtl"
  94. ? ICONS.menuClosedLeft
  95. : ICONS.menuClosedRight;
  96. const toggleLabel = open ? intlCollapse || TEXT.collapse : intlExpand || TEXT.expand;
  97. const { guid } = this;
  98. const regionId = `${guid}-region`;
  99. const buttonId = `${guid}-button`;
  100. const headerNode = toggleDisplay === "switch" ? (h("div", { "aria-controls": regionId, "aria-label": toggleLabel, class: {
  101. [CSS.toggle]: true,
  102. [CSS.toggleSwitch]: true
  103. }, id: buttonId, onClick: this.toggleSection, onKeyDown: this.handleHeaderKeyDown, tabIndex: 0, title: toggleLabel }, h("div", { class: CSS.toggleSwitchContent }, h("span", { class: CSS.toggleSwitchText }, text)), h("calcite-switch", { checked: open, label: toggleLabel, scale: "s", tabIndex: -1 }), this.renderStatusIcon())) : (h("button", { "aria-controls": regionId, "aria-label": toggleLabel, class: {
  104. [CSS.sectionHeader]: true,
  105. [CSS.toggle]: true
  106. }, id: buttonId, name: toggleLabel, onClick: this.toggleSection }, h("calcite-icon", { icon: arrowIcon, scale: "s" }), h("span", { class: CSS.sectionHeaderText }, text), this.renderStatusIcon()));
  107. return (h(Host, null, headerNode, h("section", { "aria-expanded": toAriaBoolean(open), "aria-labelledby": buttonId, class: CSS.content, hidden: !open, id: regionId }, h("slot", null))));
  108. }
  109. get el() { return this; }
  110. static get style() { return blockSectionCss; }
  111. }, [1, "calcite-block-section", {
  112. "intlCollapse": [1, "intl-collapse"],
  113. "intlExpand": [1, "intl-expand"],
  114. "open": [1540],
  115. "status": [513],
  116. "text": [1],
  117. "toggleDisplay": [513, "toggle-display"]
  118. }]);
  119. function defineCustomElement$1() {
  120. if (typeof customElements === "undefined") {
  121. return;
  122. }
  123. const components = ["calcite-block-section", "calcite-icon", "calcite-switch"];
  124. components.forEach(tagName => { switch (tagName) {
  125. case "calcite-block-section":
  126. if (!customElements.get(tagName)) {
  127. customElements.define(tagName, BlockSection);
  128. }
  129. break;
  130. case "calcite-icon":
  131. if (!customElements.get(tagName)) {
  132. defineCustomElement$3();
  133. }
  134. break;
  135. case "calcite-switch":
  136. if (!customElements.get(tagName)) {
  137. defineCustomElement$2();
  138. }
  139. break;
  140. } });
  141. }
  142. defineCustomElement$1();
  143. const CalciteBlockSection = BlockSection;
  144. const defineCustomElement = defineCustomElement$1;
  145. export { CalciteBlockSection, defineCustomElement };