[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/script-modules/block-library/accordion/ -> view.js (source)

   1  // packages/block-library/build-module/accordion/view.mjs
   2  import { store, getContext } from "@wordpress/interactivity";
   3  var hashHandled = false;
   4  var { actions } = store(
   5    "core/accordion",
   6    {
   7      state: {
   8        get isOpen() {
   9          const { id, accordionItems } = getContext();
  10          const accordionItem = accordionItems.find(
  11            (item) => item.id === id
  12          );
  13          return accordionItem ? accordionItem.isOpen : false;
  14        }
  15      },
  16      actions: {
  17        toggle: () => {
  18          const context = getContext();
  19          const { id, autoclose, accordionItems } = context;
  20          const accordionItem = accordionItems.find(
  21            (item) => item.id === id
  22          );
  23          if (autoclose) {
  24            accordionItems.forEach((item) => {
  25              item.isOpen = item.id === id ? !accordionItem.isOpen : false;
  26            });
  27          } else {
  28            accordionItem.isOpen = !accordionItem.isOpen;
  29          }
  30        },
  31        openPanelByHash: () => {
  32          if (hashHandled || !window.location?.hash?.length) {
  33            return;
  34          }
  35          const context = getContext();
  36          const { id, accordionItems, autoclose } = context;
  37          const hash = decodeURIComponent(
  38            window.location.hash.slice(1)
  39          );
  40          const targetElement = window.document.getElementById(hash);
  41          if (!targetElement) {
  42            return;
  43          }
  44          const panelElement = window.document.querySelector(
  45            '.wp-block-accordion-panel[aria-labelledby="' + id + '"]'
  46          );
  47          if (!panelElement || !panelElement.contains(targetElement)) {
  48            return;
  49          }
  50          hashHandled = true;
  51          if (autoclose) {
  52            accordionItems.forEach((item) => {
  53              item.isOpen = item.id === id;
  54            });
  55          } else {
  56            const targetItem = accordionItems.find(
  57              (item) => item.id === id
  58            );
  59            if (targetItem) {
  60              targetItem.isOpen = true;
  61            }
  62          }
  63          window.setTimeout(() => {
  64            targetElement.scrollIntoView();
  65          }, 0);
  66        }
  67      },
  68      callbacks: {
  69        initAccordionItems: () => {
  70          const context = getContext();
  71          const { id, openByDefault, accordionItems } = context;
  72          accordionItems.push({
  73            id,
  74            isOpen: openByDefault
  75          });
  76          actions.openPanelByHash();
  77        },
  78        hashChange: () => {
  79          hashHandled = false;
  80          actions.openPanelByHash();
  81        }
  82      }
  83    },
  84    { lock: true }
  85  );


Generated : Sun Jul 5 08:20:13 2026 Cross-referenced by PHPXref