UNPKG

carbon-react

Version:

A library of reusable React components for easily building user interfaces.

2 lines (1 loc) 1.17 kB
import{useState as t,useRef as e,useCallback as n}from"react";import o from"../../../__internal__/utils/helpers/events/events.js";import r from"../useMenuKeyboardNavigation/useMenuKeyboardNavigation.js";const l=(l,u=1)=>{const[i,a]=t(!1),[s,d]=t(0),c=e(null),v=e(null),p=n((()=>{a(!1)}),[]);function f(){a(!0),c.current&&d(u*c.current.getBoundingClientRect().width)}const m=n((()=>{var t;return null===(t=v.current)||void 0===t?void 0:t.querySelectorAll('[data-component="button"]')}),[]),y={"data-element":"additional-buttons",role:"list",onKeyDown:r(l,m,p,i),onBlur:t=>{t.currentTarget.contains(t.relatedTarget)||p()},minWidth:s,ref:v},g={inSplitButton:!0,onChildButtonClick:t=>e=>{var n;null==t||t(e),p(),null===(n=l.current)||void 0===n||n.focus()}};return{showAdditionalButtons:i,showButtons:f,hideButtons:p,buttonNode:c,handleToggleButtonKeyDown:t=>{const e=o.isEnterKey(t)||o.isSpaceKey(t);var n,r;!e&&!o.isDownKey(t)||i||(t.preventDefault(),f()),o.isDownKey(t)&&i&&(t.preventDefault(),null===(r=m())||void 0===r||null===(n=r[0])||void 0===n||n.focus({preventScroll:!0})),e&&i&&(t.preventDefault(),p())},wrapperProps:y,contextValue:g}};export{l as default};