|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +import {useEffect, useState} from 'react'; |
| 4 | +import {flushSync} from 'react-dom'; |
| 5 | +import type {Meta, StoryObj} from '@storybook/react'; |
| 6 | +import { |
| 7 | + BookOpenIcon as BookOpenOutline, |
| 8 | + HomeIcon as HomeOutline, |
| 9 | + Squares2X2Icon as GridOutline, |
| 10 | + UserCircleIcon as UserOutline, |
| 11 | + BellIcon as BellOutline, |
| 12 | +} from '@heroicons/react/24/outline'; |
| 13 | +import { |
| 14 | + BookOpenIcon as BookOpenSolid, |
| 15 | + HomeIcon as HomeSolid, |
| 16 | + Squares2X2Icon as GridSolid, |
| 17 | + UserCircleIcon as UserSolid, |
| 18 | + BellIcon as BellSolid, |
| 19 | +} from '@heroicons/react/24/solid'; |
| 20 | +import {TabBar, TabBarItem} from '@astryxdesign/lab'; |
| 21 | +import {Button} from '@astryxdesign/core/Button'; |
| 22 | +import {LayerProvider} from '@astryxdesign/core/Layer'; |
| 23 | +import {Icon, type IconType} from '@astryxdesign/core/Icon'; |
| 24 | + |
| 25 | +// ============================================================================= |
| 26 | +// Meta |
| 27 | +// ============================================================================= |
| 28 | + |
| 29 | +const meta: Meta<typeof TabBar> = { |
| 30 | + // The bar floats over the bottom edge: declared on LayerProvider, menus, |
| 31 | + // popovers and toasts clear it. |
| 32 | + decorators: [ |
| 33 | + Story => ( |
| 34 | + <LayerProvider inset={{blockEnd: 83}}> |
| 35 | + <Story /> |
| 36 | + </LayerProvider> |
| 37 | + ), |
| 38 | + ], |
| 39 | + title: 'Lab/TabBar', |
| 40 | + component: TabBar, |
| 41 | + parameters: { |
| 42 | + layout: 'fullscreen', |
| 43 | + }, |
| 44 | +}; |
| 45 | + |
| 46 | +export default meta; |
| 47 | +type Story = StoryObj<typeof TabBar>; |
| 48 | + |
| 49 | +// ============================================================================= |
| 50 | +// The sections UIKit's own captures were taken with: four tabs over long |
| 51 | +// scrolling screens |
| 52 | +// ============================================================================= |
| 53 | + |
| 54 | +interface Section { |
| 55 | + value: string; |
| 56 | + label: string; |
| 57 | + icon: IconType; |
| 58 | + selectedIcon: IconType; |
| 59 | +} |
| 60 | + |
| 61 | +const SECTIONS: Section[] = [ |
| 62 | + {value: 'home', label: 'Home', icon: HomeOutline, selectedIcon: HomeSolid}, |
| 63 | + { |
| 64 | + value: 'library', |
| 65 | + label: 'Library', |
| 66 | + icon: BookOpenOutline, |
| 67 | + selectedIcon: BookOpenSolid, |
| 68 | + }, |
| 69 | + { |
| 70 | + value: 'browse', |
| 71 | + label: 'Browse', |
| 72 | + icon: GridOutline, |
| 73 | + selectedIcon: GridSolid, |
| 74 | + }, |
| 75 | + { |
| 76 | + value: 'profile', |
| 77 | + label: 'Profile', |
| 78 | + icon: UserOutline, |
| 79 | + selectedIcon: UserSolid, |
| 80 | + }, |
| 81 | +]; |
| 82 | + |
| 83 | +const INBOX: Section = { |
| 84 | + value: 'inbox', |
| 85 | + label: 'Inbox', |
| 86 | + icon: BellOutline, |
| 87 | + selectedIcon: BellSolid, |
| 88 | +}; |
| 89 | + |
| 90 | +/** A screen long enough to scroll under the bar. */ |
| 91 | +function Screen({title, reselects}: {title: string; reselects: number}) { |
| 92 | + return ( |
| 93 | + <div style={{padding: '16px 16px 104px'}}> |
| 94 | + <h1 style={{margin: '8px 0 16px', fontSize: 28}}> |
| 95 | + {title || 'No tab selected'} |
| 96 | + </h1> |
| 97 | + <p data-testid="reselects">Reselected {reselects} times</p> |
| 98 | + {Array.from({length: 40}, (_, i) => ( |
| 99 | + <p key={i} style={{margin: 0, padding: '12px 0'}}> |
| 100 | + {title} row {i + 1} |
| 101 | + </p> |
| 102 | + ))} |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +function Bar({ |
| 108 | + sections = SECTIONS, |
| 109 | + initial = 'home', |
| 110 | + badges = {library: 3}, |
| 111 | + keeps = [], |
| 112 | + onChange, |
| 113 | +}: { |
| 114 | + sections?: Section[]; |
| 115 | + initial?: string; |
| 116 | + badges?: Record<string, string | number>; |
| 117 | + /** Tabs the app declines to switch to (it keeps the selection). */ |
| 118 | + keeps?: string[]; |
| 119 | + /** Also told of every choice. */ |
| 120 | + onChange?: (value: string) => void; |
| 121 | +}) { |
| 122 | + const [value, setValue] = useState(initial); |
| 123 | + const [reselects, setReselects] = useState(0); |
| 124 | + const title = sections.find(section => section.value === value)?.label ?? ''; |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <Screen title={title} reselects={reselects} /> |
| 128 | + <TabBar |
| 129 | + value={value} |
| 130 | + onChange={next => { |
| 131 | + onChange?.(next); |
| 132 | + if (!keeps.includes(next)) { |
| 133 | + setValue(next); |
| 134 | + } |
| 135 | + }} |
| 136 | + onReselect={() => setReselects(n => n + 1)} |
| 137 | + label="Sections" |
| 138 | + data-testid="tab-bar"> |
| 139 | + {sections.map(section => ( |
| 140 | + <TabBarItem |
| 141 | + key={section.value} |
| 142 | + value={section.value} |
| 143 | + label={section.label} |
| 144 | + icon={<Icon icon={section.icon} size="lg" />} |
| 145 | + selectedIcon={<Icon icon={section.selectedIcon} size="lg" />} |
| 146 | + badge={badges[section.value]} |
| 147 | + /> |
| 148 | + ))} |
| 149 | + </TabBar> |
| 150 | + </> |
| 151 | + ); |
| 152 | +} |
| 153 | + |
| 154 | +/** Four tabs, a badge on Library. Choosing Home again counts a reselect. */ |
| 155 | +export const Default: Story = { |
| 156 | + render: () => <Bar />, |
| 157 | +}; |
| 158 | + |
| 159 | +/** Badges appear, change and disappear at once, as UIKit's do. */ |
| 160 | +function BadgesDemo() { |
| 161 | + const [count, setCount] = useState(0); |
| 162 | + const badge = count === 0 ? undefined : count > 99 ? '99+' : count; |
| 163 | + return ( |
| 164 | + <> |
| 165 | + <div style={{padding: 16, display: 'flex', gap: 8}}> |
| 166 | + <Button label="Add one" onClick={() => setCount(n => n + 1)} /> |
| 167 | + <Button label="Add ten" onClick={() => setCount(n => n + 10)} /> |
| 168 | + <Button label="Clear" variant="secondary" onClick={() => setCount(0)} /> |
| 169 | + </div> |
| 170 | + <TabBarHost badges={{inbox: badge ?? '', browse: '!'}} /> |
| 171 | + </> |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +function TabBarHost({badges}: {badges: Record<string, string | number>}) { |
| 176 | + const [value, setValue] = useState('home'); |
| 177 | + return ( |
| 178 | + <TabBar |
| 179 | + value={value} |
| 180 | + onChange={setValue} |
| 181 | + label="Sections" |
| 182 | + data-testid="tab-bar"> |
| 183 | + {[SECTIONS[0], INBOX, SECTIONS[2], SECTIONS[3]].map(section => ( |
| 184 | + <TabBarItem |
| 185 | + key={section.value} |
| 186 | + value={section.value} |
| 187 | + label={section.label} |
| 188 | + icon={<Icon icon={section.icon} size="lg" />} |
| 189 | + selectedIcon={<Icon icon={section.selectedIcon} size="lg" />} |
| 190 | + badge={badges[section.value]} |
| 191 | + badgeLabel={ |
| 192 | + section.value === 'inbox' && badges.inbox !== '' |
| 193 | + ? `${badges.inbox} unread` |
| 194 | + : undefined |
| 195 | + } |
| 196 | + /> |
| 197 | + ))} |
| 198 | + </TabBar> |
| 199 | + ); |
| 200 | +} |
| 201 | + |
| 202 | +/** |
| 203 | + * The app keeps the selection when Profile is chosen (say it needs a |
| 204 | + * sign-in first): the lens settles back onto the selected tab. |
| 205 | + */ |
| 206 | +export const KeepsSelection: Story = { |
| 207 | + render: () => <Bar keeps={['profile']} />, |
| 208 | +}; |
| 209 | + |
| 210 | +/** No tab selected (the value matches none): a tap chooses one. */ |
| 211 | +export const NoSelection: Story = { |
| 212 | + render: () => <Bar initial="none" />, |
| 213 | +}; |
| 214 | + |
| 215 | +/** |
| 216 | + * The app removes the bar as a finger lifts: the lift reports nothing (each |
| 217 | + * choice would be listed on `window.__tabBarChanges`). |
| 218 | + */ |
| 219 | +function RemovedOnLiftDemo() { |
| 220 | + const [isShown, setIsShown] = useState(true); |
| 221 | + useEffect(() => { |
| 222 | + const hide = () => { |
| 223 | + const log = window as unknown as {__tabBarChanges?: string[]}; |
| 224 | + log.__tabBarChanges = [...(log.__tabBarChanges ?? []), '(removed)']; |
| 225 | + flushSync(() => setIsShown(false)); |
| 226 | + }; |
| 227 | + window.addEventListener('touchend', hide); |
| 228 | + return () => window.removeEventListener('touchend', hide); |
| 229 | + }, []); |
| 230 | + return isShown ? ( |
| 231 | + <Bar |
| 232 | + onChange={value => { |
| 233 | + const log = window as unknown as {__tabBarChanges?: string[]}; |
| 234 | + log.__tabBarChanges = [...(log.__tabBarChanges ?? []), value]; |
| 235 | + }} |
| 236 | + /> |
| 237 | + ) : ( |
| 238 | + <p style={{padding: 16}}>Removed</p> |
| 239 | + ); |
| 240 | +} |
| 241 | + |
| 242 | +export const RemovedOnLift: Story = { |
| 243 | + render: () => <RemovedOnLiftDemo />, |
| 244 | +}; |
| 245 | + |
| 246 | +/** |
| 247 | + * The app drops its last tab on the window event `tabbar:drop-last`, sent while |
| 248 | + * a finger holds the bar. The press ends without choosing a tab (each choice |
| 249 | + * would be listed on `window.__tabBarChanges`). |
| 250 | + */ |
| 251 | +function DropsLastTabDemo() { |
| 252 | + const [sections, setSections] = useState(SECTIONS); |
| 253 | + useEffect(() => { |
| 254 | + const drop = () => setSections(current => current.slice(0, -1)); |
| 255 | + window.addEventListener('tabbar:drop-last', drop); |
| 256 | + return () => window.removeEventListener('tabbar:drop-last', drop); |
| 257 | + }, []); |
| 258 | + return ( |
| 259 | + <Bar |
| 260 | + sections={sections} |
| 261 | + onChange={value => { |
| 262 | + const log = window as unknown as {__tabBarChanges?: string[]}; |
| 263 | + log.__tabBarChanges = [...(log.__tabBarChanges ?? []), value]; |
| 264 | + }} |
| 265 | + /> |
| 266 | + ); |
| 267 | +} |
| 268 | + |
| 269 | +export const DropsLastTab: Story = { |
| 270 | + render: () => <DropsLastTabDemo />, |
| 271 | +}; |
| 272 | + |
| 273 | +export const Badges: Story = { |
| 274 | + render: () => <BadgesDemo />, |
| 275 | +}; |
| 276 | + |
| 277 | +/** Five tabs: the items overlap more, as they do on iOS. */ |
| 278 | +export const FiveTabs: Story = { |
| 279 | + render: () => ( |
| 280 | + <Bar sections={[...SECTIONS.slice(0, 2), INBOX, ...SECTIONS.slice(2)]} /> |
| 281 | + ), |
| 282 | +}; |
| 283 | + |
| 284 | +/** Three tabs: the capsule narrows to its natural width and centres. */ |
| 285 | +export const ThreeTabs: Story = { |
| 286 | + render: () => <Bar sections={SECTIONS.slice(0, 3)} />, |
| 287 | +}; |
| 288 | + |
| 289 | +/** Right to left: the first tab sits on the right and the badge on the icon's top-left. */ |
| 290 | +export const RightToLeft: Story = { |
| 291 | + render: () => ( |
| 292 | + <div dir="rtl"> |
| 293 | + <Bar initial="library" /> |
| 294 | + </div> |
| 295 | + ), |
| 296 | +}; |
0 commit comments