@visa/nova-react
Version:
Visa Product Design System Nova React library
18 lines (17 loc) • 1.65 kB
JavaScript
/**
* Copyright (c) 2025 Visa, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");const t={defaultExpanded:""},r=r=>{const{defaultExpanded:a,...s}={...t,...r},d=e.useRef([]),[n,u]=e.useState(a),[l,o]=e.useState(-1),[i,c]=e.useState([]),f=-1===l?0:i.indexOf(l),p=i.length,x=p-1,y=Array.isArray(n),b=s?.ref||d,E=e.useCallback((e=>y?n.includes(e):n===e),[n,y]),A=e.useCallback((e=>{u(y?t=>E(e)?t.filter((t=>t!==e)):[...t,e]:E(e)?"":e),o(e)}),[E,y]);return e.useEffect((()=>{c(b.current?.map(((e,t)=>e&&!(e.disabled||e.ariaDisabled)&&t)).filter((e=>!1!==e)))}),[b]),{isIndexExpanded:E,onKeyNavigation:e=>{const t=e.key;let r=null;if("Tab"===t&&e.shiftKey&&f-1>=0?r=f-1:"Tab"===t&&!e.shiftKey&&f+1<=x?r=f+1:"ArrowUp"===t?r=f-1<0?x:f-1:"ArrowDown"===t?r=(f+1)%p:"End"===t?r=x:"Home"===t&&(r=0),b.current&&null!==r&&p){l!==r&&e.preventDefault();const t=i[r];o(t),b.current[t]?.focus()}},ref:b,toggleIndexExpanded:A}};r.displayName="useAccordion",r.defaultProps={defaultExpanded:""},exports.default=r,exports.useAccordion=r;