initial
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
// packages/block-library/build-module/accordion/view.mjs
|
||||
import { store, getContext, withSyncEvent } from "@wordpress/interactivity";
|
||||
var hashHandled = false;
|
||||
var { actions } = store(
|
||||
"core/accordion",
|
||||
{
|
||||
state: {
|
||||
get isOpen() {
|
||||
const { id, accordionItems } = getContext();
|
||||
const accordionItem = accordionItems.find(
|
||||
(item) => item.id === id
|
||||
);
|
||||
return accordionItem ? accordionItem.isOpen : false;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
toggle: () => {
|
||||
const context = getContext();
|
||||
const { id, autoclose, accordionItems } = context;
|
||||
const accordionItem = accordionItems.find(
|
||||
(item) => item.id === id
|
||||
);
|
||||
if (autoclose) {
|
||||
accordionItems.forEach((item) => {
|
||||
item.isOpen = item.id === id ? !accordionItem.isOpen : false;
|
||||
});
|
||||
} else {
|
||||
accordionItem.isOpen = !accordionItem.isOpen;
|
||||
}
|
||||
},
|
||||
handleKeyDown: withSyncEvent((event) => {
|
||||
if (event.key !== "ArrowUp" && event.key !== "ArrowDown" && event.key !== "Home" && event.key !== "End") {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
const context = getContext();
|
||||
const { id, accordionItems } = context;
|
||||
const currentIndex = accordionItems.findIndex(
|
||||
(item) => item.id === id
|
||||
);
|
||||
let nextIndex;
|
||||
switch (event.key) {
|
||||
case "ArrowUp":
|
||||
nextIndex = Math.max(0, currentIndex - 1);
|
||||
break;
|
||||
case "ArrowDown":
|
||||
nextIndex = Math.min(
|
||||
currentIndex + 1,
|
||||
accordionItems.length - 1
|
||||
);
|
||||
break;
|
||||
case "Home":
|
||||
nextIndex = 0;
|
||||
break;
|
||||
case "End":
|
||||
nextIndex = accordionItems.length - 1;
|
||||
break;
|
||||
}
|
||||
const nextId = accordionItems[nextIndex].id;
|
||||
const nextButton = document.getElementById(nextId);
|
||||
if (nextButton) {
|
||||
nextButton.focus();
|
||||
}
|
||||
}),
|
||||
openPanelByHash: () => {
|
||||
if (hashHandled || !window.location?.hash?.length) {
|
||||
return;
|
||||
}
|
||||
const context = getContext();
|
||||
const { id, accordionItems, autoclose } = context;
|
||||
const hash = decodeURIComponent(
|
||||
window.location.hash.slice(1)
|
||||
);
|
||||
const targetElement = window.document.getElementById(hash);
|
||||
if (!targetElement) {
|
||||
return;
|
||||
}
|
||||
const panelElement = window.document.querySelector(
|
||||
'.wp-block-accordion-panel[aria-labelledby="' + id + '"]'
|
||||
);
|
||||
if (!panelElement || !panelElement.contains(targetElement)) {
|
||||
return;
|
||||
}
|
||||
hashHandled = true;
|
||||
if (autoclose) {
|
||||
accordionItems.forEach((item) => {
|
||||
item.isOpen = item.id === id;
|
||||
});
|
||||
} else {
|
||||
const targetItem = accordionItems.find(
|
||||
(item) => item.id === id
|
||||
);
|
||||
if (targetItem) {
|
||||
targetItem.isOpen = true;
|
||||
}
|
||||
}
|
||||
window.setTimeout(() => {
|
||||
targetElement.scrollIntoView();
|
||||
}, 0);
|
||||
}
|
||||
},
|
||||
callbacks: {
|
||||
initAccordionItems: () => {
|
||||
const context = getContext();
|
||||
const { id, openByDefault, accordionItems } = context;
|
||||
accordionItems.push({
|
||||
id,
|
||||
isOpen: openByDefault
|
||||
});
|
||||
actions.openPanelByHash();
|
||||
},
|
||||
hashChange: () => {
|
||||
hashHandled = false;
|
||||
actions.openPanelByHash();
|
||||
}
|
||||
}
|
||||
},
|
||||
{ lock: true }
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
<?php return array('dependencies' => array(), 'module_dependencies' => array(array('id' => '@wordpress/interactivity', 'import' => 'static')), 'version' => '2af01b43d30739c3fb8d');
|
||||
@@ -0,0 +1 @@
|
||||
import{store as f,getContext as i,withSyncEvent as m}from"@wordpress/interactivity";var l=!1,{actions:h}=f("core/accordion",{state:{get isOpen(){let{id:e,accordionItems:t}=i(),c=t.find(o=>o.id===e);return c?c.isOpen:!1}},actions:{toggle:()=>{let e=i(),{id:t,autoclose:c,accordionItems:o}=e,s=o.find(n=>n.id===t);c?o.forEach(n=>{n.isOpen=n.id===t?!s.isOpen:!1}):s.isOpen=!s.isOpen},handleKeyDown:m(e=>{if(e.key!=="ArrowUp"&&e.key!=="ArrowDown"&&e.key!=="Home"&&e.key!=="End")return;e.preventDefault();let t=i(),{id:c,accordionItems:o}=t,s=o.findIndex(d=>d.id===c),n;switch(e.key){case"ArrowUp":n=Math.max(0,s-1);break;case"ArrowDown":n=Math.min(s+1,o.length-1);break;case"Home":n=0;break;case"End":n=o.length-1;break}let r=o[n].id,a=document.getElementById(r);a&&a.focus()}),openPanelByHash:()=>{if(l||!window.location?.hash?.length)return;let e=i(),{id:t,accordionItems:c,autoclose:o}=e,s=decodeURIComponent(window.location.hash.slice(1)),n=window.document.getElementById(s);if(!n)return;let r=window.document.querySelector('.wp-block-accordion-panel[aria-labelledby="'+t+'"]');if(!(!r||!r.contains(n))){if(l=!0,o)c.forEach(a=>{a.isOpen=a.id===t});else{let a=c.find(d=>d.id===t);a&&(a.isOpen=!0)}window.setTimeout(()=>{n.scrollIntoView()},0)}}},callbacks:{initAccordionItems:()=>{let e=i(),{id:t,openByDefault:c,accordionItems:o}=e;o.push({id:t,isOpen:c}),h.openPanelByHash()},hashChange:()=>{l=!1,h.openPanelByHash()}}},{lock:!0});
|
||||
Reference in New Issue
Block a user