| 37 | useContext(OverflowActionsContext); |
| 38 | |
| 39 | export const OverflowProvider: React.FC<{ children: React.ReactNode }> = ({ |
| 40 | children, |
| 41 | }) => { |
| 42 | const [overflowingIds, setOverflowingIds] = useState(new Set<string>()); |
| 43 | |
| 44 | const addOverflowingId = useCallback((id: string) => { |
| 45 | setOverflowingIds((prevIds) => { |
| 46 | if (prevIds.has(id)) { |
| 47 | return prevIds; |
| 48 | } |
| 49 | const newIds = new Set(prevIds); |
| 50 | newIds.add(id); |
| 51 | return newIds; |
| 52 | }); |
| 53 | }, []); |
| 54 | |
| 55 | const removeOverflowingId = useCallback((id: string) => { |
| 56 | setOverflowingIds((prevIds) => { |
| 57 | if (!prevIds.has(id)) { |
| 58 | return prevIds; |
| 59 | } |
| 60 | const newIds = new Set(prevIds); |
| 61 | newIds.delete(id); |
| 62 | return newIds; |
| 63 | }); |
| 64 | }, []); |
| 65 | |
| 66 | const stateValue = useMemo( |
| 67 | () => ({ |
| 68 | overflowingIds, |
| 69 | }), |
| 70 | [overflowingIds], |
| 71 | ); |
| 72 | |
| 73 | const actionsValue = useMemo( |
| 74 | () => ({ |
| 75 | addOverflowingId, |
| 76 | removeOverflowingId, |
| 77 | }), |
| 78 | [addOverflowingId, removeOverflowingId], |
| 79 | ); |
| 80 | |
| 81 | return ( |
| 82 | <OverflowStateContext.Provider value={stateValue}> |
| 83 | <OverflowActionsContext.Provider value={actionsValue}> |
| 84 | {children} |
| 85 | </OverflowActionsContext.Provider> |
| 86 | </OverflowStateContext.Provider> |
| 87 | ); |
| 88 | }; |