calcite-block-section.js 9.6 KB

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