ExpandToggle.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 { h } from "@stencil/core";
  7. import { getElementDir } from "../../utils/dom";
  8. import { queryActions } from "../action-bar/utils";
  9. import { SLOTS as ACTION_GROUP_SLOTS } from "../action-group/resources";
  10. const ICONS = {
  11. chevronsLeft: "chevrons-left",
  12. chevronsRight: "chevrons-right"
  13. };
  14. function getCalcitePosition(position, el) {
  15. var _a;
  16. return position || ((_a = el.closest("calcite-shell-panel")) === null || _a === void 0 ? void 0 : _a.position) || "start";
  17. }
  18. export function toggleChildActionText({ parent, expanded }) {
  19. queryActions(parent)
  20. .filter((el) => el.slot !== ACTION_GROUP_SLOTS.menuActions)
  21. .forEach((action) => (action.textEnabled = expanded));
  22. parent
  23. .querySelectorAll("calcite-action-group, calcite-action-menu")
  24. .forEach((el) => (el.expanded = expanded));
  25. }
  26. const setTooltipReference = ({ tooltip, referenceElement, expanded, ref }) => {
  27. if (tooltip) {
  28. tooltip.referenceElement = !expanded && referenceElement ? referenceElement : null;
  29. }
  30. if (ref) {
  31. ref(referenceElement);
  32. }
  33. return referenceElement;
  34. };
  35. export const ExpandToggle = ({ expanded, intlExpand, intlCollapse, toggle, el, position, tooltip, ref, scale }) => {
  36. const rtl = getElementDir(el) === "rtl";
  37. const expandText = expanded ? intlCollapse : intlExpand;
  38. const icons = [ICONS.chevronsLeft, ICONS.chevronsRight];
  39. if (rtl) {
  40. icons.reverse();
  41. }
  42. const end = getCalcitePosition(position, el) === "end";
  43. const expandIcon = end ? icons[1] : icons[0];
  44. const collapseIcon = end ? icons[0] : icons[1];
  45. const actionNode = (h("calcite-action", { icon: expanded ? expandIcon : collapseIcon, onClick: toggle, ref: (referenceElement) => setTooltipReference({ tooltip, referenceElement, expanded, ref }), scale: scale, text: expandText, textEnabled: expanded }));
  46. return actionNode;
  47. };