Skip to content

Commit

Permalink
Use ResizeObserver instead of observing the window width
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 26, 2023
1 parent e27e9b0 commit 3a52d23
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
1 change: 1 addition & 0 deletions code/ui/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@storybook/theming": "7.0.0-beta.33",
"@storybook/types": "7.0.0-beta.33",
"memoizerific": "^1.11.3",
"use-resize-observer": "^9.1.0",
"util-deprecate": "^1.0.2"
},
"devDependencies": {
Expand Down
9 changes: 0 additions & 9 deletions code/ui/components/src/hooks/useOnWindowResize.tsx

This file was deleted.

23 changes: 11 additions & 12 deletions code/ui/components/src/tabs/tabs.hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react';
import { sanitize } from '@storybook/csf';
import { styled } from '@storybook/theming';
import useResizeObserver from 'use-resize-observer';
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';
Expand Down Expand Up @@ -36,6 +36,9 @@ export function useList(list: ChildrenList) {
const tabBarRef = useRef<HTMLDivElement>();
const addonsRef = useRef<HTMLButtonElement>();
const tabRefs = useRef(new Map<string, HTMLButtonElement>());
const { width: tabBarWidth = 1 } = useResizeObserver<HTMLDivElement>({
ref: tabBarRef,
});

const [visibleList, setVisibleList] = useState(list);
const [invisibleList, setInvisibleList] = useState<ChildrenList>([]);
Expand All @@ -60,14 +63,14 @@ export function useList(list: ChildrenList) {
withArrows={false}
visible={isTooltipVisible}
onVisibleChange={setTooltipVisible}
placement="bottom"
delayHide={100}
tooltip={
<TooltipLinkList
links={invisibleList.map(({ title, id, color, active }) => {
const tabTitle = typeof title === 'function' ? title() : title;
return {
id,
title: tabTitle,
title,
color,
active,
onClick: (e) => {
Expand Down Expand Up @@ -96,22 +99,21 @@ export function useList(list: ChildrenList) {
</AddonButton>
</WithTooltip>
{invisibleList.map(({ title, id, color }) => {
const tabTitle = typeof title === 'function' ? title() : title;
return (
<TabButton
id={`tabbutton-${sanitize(tabTitle)}`}
id={`tabbutton-${sanitize(title)}`}
style={{ visibility: 'hidden' }}
tabIndex={-1}
ref={(ref: HTMLButtonElement) => {
tabRefs.current.set(tabTitle, ref);
tabRefs.current.set(title, ref);
}}
className="tabbutton"
type="button"
key={id}
textColor={color}
role="tab"
>
{tabTitle}
{title}
</TabButton>
);
})}
Expand All @@ -133,8 +135,7 @@ export function useList(list: ChildrenList) {

const newInvisibleList = list.filter((item) => {
const { title } = item;
const tabTitle = typeof title === 'function' ? title() : title;
const tabButton = tabRefs.current.get(tabTitle);
const tabButton = tabRefs.current.get(title);

if (!tabButton) {
return false;
Expand All @@ -159,9 +160,7 @@ export function useList(list: ChildrenList) {
}
}, [invisibleList.length, list, visibleList]);

useOnWindowResize(setTabLists);

useLayoutEffect(setTabLists, [setTabLists]);
useLayoutEffect(setTabLists, [setTabLists, tabBarWidth]);

return {
tabRefs,
Expand Down
15 changes: 1 addition & 14 deletions code/ui/components/src/tabs/tabs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,7 @@ const content = Object.entries(panels).map(([k, v]) => (

export default {
title: 'Tabs',
decorators: [
(story) => (
<div
style={{
position: 'relative',
height: 'calc(100vh - 20px)',
width: 'calc(100vw - 20px)',
margin: 10,
}}
>
{story()}
</div>
),
],
decorators: [(story) => <div>{story()}</div>],
args: {
menuName: 'Addons',
},
Expand Down
8 changes: 3 additions & 5 deletions code/ui/components/src/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,11 @@ export const Tabs: FC<TabsProps> = memo(
<WrapperChildren backgroundColor={backgroundColor}>
<TabBar ref={tabBarRef} role="tablist">
{visibleList.map(({ title, id, active, color }, index) => {
const tabTitle = typeof title === 'function' ? title() : title;

return (
<TabButton
id={`tabbutton-${sanitize(tabTitle)}`}
id={`tabbutton-${sanitize(title)}`}
ref={(ref: HTMLButtonElement) => {
tabRefs.current.set(tabTitle, ref);
tabRefs.current.set(title, ref);
}}
className={`tabbutton ${active ? 'tabbutton-active' : ''}`}
type="button"
Expand All @@ -172,7 +170,7 @@ export const Tabs: FC<TabsProps> = memo(
}}
role="tab"
>
{tabTitle}
{title}
</TabButton>
);
})}
Expand Down
2 changes: 1 addition & 1 deletion code/ui/manager/src/components/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const AddonPanel = React.memo<{
id="storybook-panel-root"
>
{Object.entries(panels).map(([k, v]) => (
<SafeTab key={k} id={k} title={v.title}>
<SafeTab key={k} id={k} title={typeof v.title === 'function' ? v.title() : v.title}>
{v.render}
</SafeTab>
))}
Expand Down
20 changes: 20 additions & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3092,6 +3092,13 @@ __metadata:
languageName: node
linkType: hard

"@juggle/resize-observer@npm:^3.3.1":
version: 3.4.0
resolution: "@juggle/resize-observer@npm:3.4.0"
checksum: 12930242357298c6f2ad5d4ec7cf631dfb344ca7c8c830ab7f64e6ac11eb1aae486901d8d880fd08fb1b257800c160a0da3aee1e7ed9adac0ccbb9b7c5d93347
languageName: node
linkType: hard

"@leichtgewicht/ip-codec@npm:^2.0.1":
version: 2.0.4
resolution: "@leichtgewicht/ip-codec@npm:2.0.4"
Expand Down Expand Up @@ -6025,6 +6032,7 @@ __metadata:
react-textarea-autosize: ^8.3.0
ts-dedent: ^2.0.0
typescript: ~4.9.3
use-resize-observer: ^9.1.0
util-deprecate: ^1.0.2
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0
Expand Down Expand Up @@ -27860,6 +27868,18 @@ __metadata:
languageName: node
linkType: hard

"use-resize-observer@npm:^9.1.0":
version: 9.1.0
resolution: "use-resize-observer@npm:9.1.0"
dependencies:
"@juggle/resize-observer": ^3.3.1
peerDependencies:
react: 16.8.0 - 18
react-dom: 16.8.0 - 18
checksum: 6ccdeb09fe20566ec182b1635a22f189e13d46226b74610432590e69b31ef5d05d069badc3306ebd0d2bb608743b17981fb535763a1d7dc2c8ae462ee8e5999c
languageName: node
linkType: hard

"use@npm:^3.1.0":
version: 3.1.1
resolution: "use@npm:3.1.1"
Expand Down

0 comments on commit 3a52d23

Please sign in to comment.