-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbc8f9b
commit 04b0a59
Showing
23 changed files
with
491 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useEffect } from 'react'; | ||
|
||
export function useOnWindowResize(cb: (ev: UIEvent) => void) { | ||
useEffect(() => { | ||
window.addEventListener('resize', cb); | ||
|
||
return () => window.removeEventListener('resize', cb); | ||
}, [cb]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { styled } from '@storybook/theming'; | ||
import type { ReactElement } from 'react'; | ||
import React, { Children } from 'react'; | ||
|
||
export interface VisuallyHiddenProps { | ||
active?: boolean; | ||
} | ||
|
||
export const VisuallyHidden = styled.div<VisuallyHiddenProps>(({ active }) => | ||
active ? { display: 'block' } : { display: 'none' } | ||
); | ||
|
||
export const childrenToList = (children: any, selected: string) => | ||
Children.toArray(children).map( | ||
({ props: { title, id, color, children: childrenOfChild } }: ReactElement, index) => { | ||
const content = Array.isArray(childrenOfChild) ? childrenOfChild[0] : childrenOfChild; | ||
return { | ||
active: selected ? id === selected : index === 0, | ||
title, | ||
id, | ||
color, | ||
render: | ||
typeof content === 'function' | ||
? content | ||
: ({ active, key }: any) => ( | ||
<VisuallyHidden key={key} active={active} role="tabpanel"> | ||
{content} | ||
</VisuallyHidden> | ||
), | ||
}; | ||
} | ||
); | ||
|
||
export type ChildrenList = ReturnType<typeof childrenToList>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react'; | ||
import { sanitize } from '@storybook/csf'; | ||
import { styled } from '@storybook/theming'; | ||
import { TabButton } from '../bar/button'; | ||
import { useOnWindowResize } from '../hooks/useOnWindowResize'; | ||
import { TooltipLinkList } from '../tooltip/TooltipLinkList'; | ||
import { WithTooltip } from '../tooltip/WithTooltip'; | ||
import type { ChildrenList } from './tabs.helpers'; | ||
import type { Link } from '../tooltip/TooltipLinkList'; | ||
|
||
const CollapseIcon = styled.span<{ isActive: boolean }>(({ theme, isActive }) => ({ | ||
display: 'inline-block', | ||
width: 0, | ||
height: 0, | ||
marginLeft: 8, | ||
color: isActive ? theme.color.secondary : theme.color.mediumdark, | ||
borderRight: '3px solid transparent', | ||
borderLeft: `3px solid transparent`, | ||
borderTop: '3px solid', | ||
transition: 'transform .1s ease-out', | ||
})); | ||
|
||
const AddonButton = styled(TabButton)<{ preActive: boolean }>(({ active, theme, preActive }) => { | ||
return ` | ||
color: ${preActive || active ? theme.color.secondary : theme.color.mediumdark}; | ||
&:hover { | ||
color: ${theme.color.secondary}; | ||
.addon-collapsible-icon { | ||
color: ${theme.color.secondary}; | ||
} | ||
} | ||
`; | ||
}); | ||
|
||
export function useList(list: ChildrenList) { | ||
const tabBarRef = useRef<HTMLDivElement>(); | ||
const addonsRef = useRef<HTMLButtonElement>(); | ||
const tabRefs = useRef(new Map<string, HTMLButtonElement>()); | ||
|
||
const [visibleList, setVisibleList] = useState(list); | ||
const [invisibleList, setInvisibleList] = useState<ChildrenList>([]); | ||
const previousList = useRef<ChildrenList>(list); | ||
|
||
const AddonTab = useCallback( | ||
({ | ||
menuName, | ||
actions, | ||
}: { | ||
menuName: string; | ||
actions?: { | ||
onSelect: (id: string) => void; | ||
} & Record<string, any>; | ||
}) => { | ||
const isAddonsActive = invisibleList.some(({ active }) => active); | ||
const [isTooltipVisible, setTooltipVisible] = useState(false); | ||
return ( | ||
<> | ||
<WithTooltip | ||
interactive | ||
withArrows={false} | ||
visible={isTooltipVisible} | ||
onVisibleChange={setTooltipVisible} | ||
delayHide={100} | ||
tooltip={ | ||
<TooltipLinkList | ||
links={invisibleList.map(({ title, id, color, active }) => { | ||
const tabTitle = typeof title === 'function' ? title() : title; | ||
return { | ||
id, | ||
title: tabTitle, | ||
color, | ||
active, | ||
onClick: (e) => { | ||
e.preventDefault(); | ||
actions.onSelect(id); | ||
}, | ||
} as Link; | ||
})} | ||
/> | ||
} | ||
> | ||
<AddonButton | ||
ref={addonsRef} | ||
active={isAddonsActive} | ||
preActive={isTooltipVisible} | ||
style={{ visibility: invisibleList.length ? 'visible' : 'hidden' }} | ||
className="tabbutton" | ||
type="button" | ||
role="tab" | ||
> | ||
{menuName} | ||
<CollapseIcon | ||
className="addon-collapsible-icon" | ||
isActive={isAddonsActive || isTooltipVisible} | ||
/> | ||
</AddonButton> | ||
</WithTooltip> | ||
{invisibleList.map(({ title, id, color }) => { | ||
const tabTitle = typeof title === 'function' ? title() : title; | ||
return ( | ||
<TabButton | ||
id={`tabbutton-${sanitize(tabTitle)}`} | ||
style={{ visibility: 'hidden' }} | ||
tabIndex={-1} | ||
ref={(ref: HTMLButtonElement) => { | ||
tabRefs.current.set(tabTitle, ref); | ||
}} | ||
className="tabbutton" | ||
type="button" | ||
key={id} | ||
textColor={color} | ||
role="tab" | ||
> | ||
{tabTitle} | ||
</TabButton> | ||
); | ||
})} | ||
</> | ||
); | ||
}, | ||
[invisibleList] | ||
); | ||
|
||
const setTabLists = useCallback(() => { | ||
// get x and width from tabBarRef div | ||
const { x, width } = tabBarRef.current.getBoundingClientRect(); | ||
const { width: widthAddonsTab } = addonsRef.current.getBoundingClientRect(); | ||
const rightBorder = invisibleList.length ? x + width - widthAddonsTab : x + width; | ||
|
||
const newVisibleList: ChildrenList = []; | ||
|
||
let widthSum = 0; | ||
|
||
const newInvisibleList = list.filter((item) => { | ||
const { title } = item; | ||
const tabTitle = typeof title === 'function' ? title() : title; | ||
const tabButton = tabRefs.current.get(tabTitle); | ||
|
||
if (!tabButton) { | ||
return false; | ||
} | ||
const { width: tabWidth } = tabButton.getBoundingClientRect(); | ||
|
||
const crossBorder = x + widthSum + tabWidth > rightBorder; | ||
|
||
if (!crossBorder) { | ||
newVisibleList.push(item); | ||
} | ||
|
||
widthSum += tabWidth; | ||
|
||
return crossBorder; | ||
}); | ||
|
||
if (newVisibleList.length !== visibleList.length || previousList.current !== list) { | ||
setVisibleList(newVisibleList); | ||
setInvisibleList(newInvisibleList); | ||
previousList.current = list; | ||
} | ||
}, [invisibleList.length, list, visibleList]); | ||
|
||
useOnWindowResize(setTabLists); | ||
|
||
useLayoutEffect(setTabLists, [setTabLists]); | ||
|
||
return { | ||
tabRefs, | ||
addonsRef, | ||
tabBarRef, | ||
visibleList, | ||
invisibleList, | ||
AddonTab, | ||
}; | ||
} |
Oops, something went wrong.