11// Copyright (c) Meta Platforms, Inc. and affiliates.
22
3- import { useEffect , useState } from 'react' ;
3+ import { useEffect , useRef , useState } from 'react' ;
44import { flushSync } from 'react-dom' ;
55import type { Meta , StoryObj } from '@storybook/react' ;
66import {
@@ -9,15 +9,29 @@ import {
99 Squares2X2Icon as GridOutline ,
1010 UserCircleIcon as UserOutline ,
1111 BellIcon as BellOutline ,
12+ Cog6ToothIcon as CogOutline ,
13+ QuestionMarkCircleIcon as HelpOutline ,
1214} from '@heroicons/react/24/outline' ;
1315import {
1416 BookOpenIcon as BookOpenSolid ,
1517 HomeIcon as HomeSolid ,
1618 Squares2X2Icon as GridSolid ,
1719 UserCircleIcon as UserSolid ,
1820 BellIcon as BellSolid ,
21+ Cog6ToothIcon as CogSolid ,
22+ QuestionMarkCircleIcon as HelpSolid ,
1923} from '@heroicons/react/24/solid' ;
20- import { TabBar , TabBarItem } from '@astryxdesign/lab' ;
24+ import { ForwardIcon , PauseIcon , PlayIcon } from '@heroicons/react/24/solid' ;
25+ import { MagnifyingGlassIcon } from '@heroicons/react/24/outline' ;
26+ import {
27+ TabBar ,
28+ TabBarItem ,
29+ TabBarMoreList ,
30+ TabBarSearchTab ,
31+ SearchField ,
32+ type SearchFieldHandle ,
33+ type TabBarAccessoryPlacement ,
34+ } from '@astryxdesign/lab' ;
2135import { Button } from '@astryxdesign/core/Button' ;
2236import { LayerProvider } from '@astryxdesign/core/Layer' ;
2337import { Icon , type IconType } from '@astryxdesign/core/Icon' ;
@@ -110,6 +124,11 @@ function Bar({
110124 badges = { library : 3 } ,
111125 keeps = [ ] ,
112126 onChange,
127+ minimizeBehavior,
128+ bottomAccessory,
129+ hasSearch = false ,
130+ searchActivates,
131+ disabled = [ ] ,
113132} : {
114133 sections ?: Section [ ] ;
115134 initial ?: string ;
@@ -118,10 +137,32 @@ function Bar({
118137 keeps ?: string [ ] ;
119138 /** Also told of every choice. */
120139 onChange ?: ( value : string ) => void ;
140+ minimizeBehavior ?: 'never' | 'onScrollDown' ;
141+ bottomAccessory ?: ( placement : TabBarAccessoryPlacement ) => React . ReactNode ;
142+ hasSearch ?: boolean ;
143+ /** The search tab's `automaticallyActivates`. */
144+ searchActivates ?: boolean ;
145+ disabled ?: string [ ] ;
121146} ) {
122147 const [ value , setValue ] = useState ( initial ) ;
123148 const [ reselects , setReselects ] = useState ( 0 ) ;
124- const title = sections . find ( section => section . value === value ) ?. label ?? '' ;
149+ const [ isSearchActive , setIsSearchActive ] = useState ( false ) ;
150+ const fieldRef = useRef < SearchFieldHandle | null > ( null ) ;
151+ const [ query , setQuery ] = useState ( '' ) ;
152+ // Cancelling search returns to the tab it was chosen from.
153+ const lastTabRef = useRef ( initial ) ;
154+ if ( value !== 'search' ) {
155+ lastTabRef . current = value ;
156+ }
157+ const setSearchActive = ( isActive : boolean ) => {
158+ setIsSearchActive ( isActive ) ;
159+ if ( ! isActive ) {
160+ setValue ( lastTabRef . current ) ;
161+ }
162+ } ;
163+ const title =
164+ sections . find ( section => section . value === value ) ?. label ??
165+ ( value === 'search' ? 'Search' : '' ) ;
125166 return (
126167 < >
127168 < Screen title = { title } reselects = { reselects } />
@@ -135,6 +176,8 @@ function Bar({
135176 } }
136177 onReselect = { ( ) => setReselects ( n => n + 1 ) }
137178 label = "Sections"
179+ minimizeBehavior = { minimizeBehavior }
180+ bottomAccessory = { bottomAccessory }
138181 data-testid = "tab-bar" >
139182 { sections . map ( section => (
140183 < TabBarItem
@@ -144,8 +187,30 @@ function Bar({
144187 icon = { < Icon icon = { section . icon } size = "lg" /> }
145188 selectedIcon = { < Icon icon = { section . selectedIcon } size = "lg" /> }
146189 badge = { badges [ section . value ] }
190+ isDisabled = { disabled . includes ( section . value ) }
147191 />
148192 ) ) }
193+ { hasSearch ? (
194+ < TabBarSearchTab
195+ value = "search"
196+ label = "Search"
197+ icon = { < Icon icon = { MagnifyingGlassIcon } size = "lg" /> }
198+ isActive = { isSearchActive }
199+ onActiveChange = { setSearchActive }
200+ automaticallyActivates = { searchActivates }
201+ fieldRef = { fieldRef } >
202+ < SearchField
203+ placement = "tab"
204+ label = "Search"
205+ placeholder = "Search"
206+ value = { query }
207+ onChange = { setQuery }
208+ isActive = { isSearchActive }
209+ onActiveChange = { setSearchActive }
210+ handleRef = { fieldRef }
211+ />
212+ </ TabBarSearchTab >
213+ ) : null }
149214 </ TabBar >
150215 </ >
151216 ) ;
@@ -156,6 +221,119 @@ export const Default: Story = {
156221 render : ( ) => < Bar /> ,
157222} ;
158223
224+ /**
225+ * Scroll the page down under a finger: the bar collapses into a circle with
226+ * the selected icon. Scroll back to the top, or tap the circle, to expand it.
227+ */
228+ export const MinimizeOnScroll : Story = {
229+ render : ( ) => < Bar initial = "library" minimizeBehavior = "onScrollDown" /> ,
230+ } ;
231+
232+ /** A now-playing bar for the bottom accessory; inline, it drops "Next". */
233+ function NowPlaying ( { placement} : { placement : TabBarAccessoryPlacement } ) {
234+ const [ isPlaying , setIsPlaying ] = useState ( false ) ;
235+ return (
236+ < div
237+ style = { {
238+ display : 'flex' ,
239+ alignItems : 'center' ,
240+ gap : 10 ,
241+ width : '100%' ,
242+ padding : '0 8px 0 6px' ,
243+ boxSizing : 'border-box' ,
244+ } } >
245+ < div
246+ aria-hidden
247+ style = { {
248+ width : 36 ,
249+ height : 36 ,
250+ flexShrink : 0 ,
251+ borderRadius : 8 ,
252+ background : 'linear-gradient(135deg, #f97316, #db2777)' ,
253+ } }
254+ />
255+ < div
256+ style = { {
257+ flex : 1 ,
258+ minWidth : 0 ,
259+ overflow : 'hidden' ,
260+ textOverflow : 'ellipsis' ,
261+ whiteSpace : 'nowrap' ,
262+ fontSize : 14 ,
263+ } } >
264+ Midnight City
265+ </ div >
266+ < Button
267+ label = { isPlaying ? 'Pause' : 'Play' }
268+ variant = "ghost"
269+ isIconOnly
270+ icon = { < Icon icon = { isPlaying ? PauseIcon : PlayIcon } /> }
271+ onClick = { ( ) => setIsPlaying ( playing => ! playing ) }
272+ />
273+ { placement === 'expanded' ? (
274+ < Button
275+ label = "Next"
276+ variant = "ghost"
277+ isIconOnly
278+ icon = { < Icon icon = { ForwardIcon } /> }
279+ />
280+ ) : null }
281+ </ div >
282+ ) ;
283+ }
284+
285+ /**
286+ * A bottom accessory above the bar. Scroll the page down under a finger: it
287+ * moves down inline beside the circle, and back up at the top. The stack is
288+ * 139 px tall (the bar's 83 and the accessory's 56), declared on
289+ * LayerProvider so menus, popovers and toasts clear it.
290+ */
291+ export const BottomAccessory : Story = {
292+ render : ( ) => (
293+ < LayerProvider inset = { { blockEnd : 139 } } >
294+ < Bar
295+ initial = "library"
296+ minimizeBehavior = "onScrollDown"
297+ bottomAccessory = { placement => < NowPlaying placement = { placement } /> }
298+ />
299+ </ LayerProvider >
300+ ) ,
301+ } ;
302+
303+ /**
304+ * A search tab: a circle at the trailing end, apart from the tabs, which share
305+ * a narrower capsule. Choosing it collapses the bar to a circle with the tab it
306+ * was chosen from, and raises its field above the keyboard. The search circle
307+ * beside it is the cancel button, which returns to that tab. Scroll down under
308+ * a finger: both circles stay.
309+ */
310+ export const SearchTab : Story = {
311+ render : ( ) => (
312+ < Bar
313+ hasSearch
314+ minimizeBehavior = "onScrollDown"
315+ bottomAccessory = { placement => < NowPlaying placement = { placement } /> }
316+ />
317+ ) ,
318+ } ;
319+
320+ /**
321+ * A search tab that waits inline (`automaticallyActivates={false}`):
322+ * choosing it shows the field in the bar; a tap on the field activates it.
323+ */
324+ export const SearchTabWaitsInline : Story = {
325+ render : ( ) => < Bar hasSearch searchActivates = { false } /> ,
326+ } ;
327+
328+ /**
329+ * The app has disabled the selected tab (Home) and search is chosen from it.
330+ * Leaving search returns to the enabled tab nearest it (Library), never to the
331+ * disabled one.
332+ */
333+ export const SearchFromDisabledTab : Story = {
334+ render : ( ) => < Bar hasSearch searchActivates = { false } disabled = { [ 'home' ] } /> ,
335+ } ;
336+
159337/** Badges appear, change and disappear at once, as UIKit's do. */
160338function BadgesDemo ( ) {
161339 const [ count , setCount ] = useState ( 0 ) ;
@@ -274,6 +452,121 @@ export const Badges: Story = {
274452 render : ( ) => < BadgesDemo /> ,
275453} ;
276454
455+ /** Browse is disabled: dimmed, and a tap, the keys or the lens leave it unchosen. */
456+ export const DisabledTab : Story = {
457+ render : ( ) => < Bar disabled = { [ 'browse' ] } /> ,
458+ } ;
459+
460+ /** Six sections: the four the bar holds, then two that overflow into More. */
461+ const SIX : Section [ ] = [
462+ ...SECTIONS ,
463+ {
464+ value : 'settings' ,
465+ label : 'Settings' ,
466+ icon : CogOutline ,
467+ selectedIcon : CogSolid ,
468+ } ,
469+ { value : 'help' , label : 'Help' , icon : HelpOutline , selectedIcon : HelpSolid } ,
470+ ] ;
471+
472+ function MoreDemo ( { hasSearch = false } : { hasSearch ?: boolean } ) {
473+ const [ value , setValue ] = useState ( 'home' ) ;
474+ const [ reselects , setReselects ] = useState ( 0 ) ;
475+ const [ isSearchActive , setIsSearchActive ] = useState ( false ) ;
476+ const fieldRef = useRef < SearchFieldHandle | null > ( null ) ;
477+ const [ query , setQuery ] = useState ( '' ) ;
478+ const tabs = SIX . map ( section => (
479+ < TabBarItem
480+ key = { section . value }
481+ value = { section . value }
482+ label = { section . label }
483+ icon = { < Icon icon = { section . icon } size = "lg" /> }
484+ selectedIcon = { < Icon icon = { section . selectedIcon } size = "lg" /> }
485+ badge = { section . value === 'help' ? 2 : undefined }
486+ />
487+ ) ) ;
488+ const inMore = SIX . slice ( 4 ) . find ( section => section . value === value ) ;
489+ return (
490+ < >
491+ { value === 'more' ? (
492+ < div style = { { padding : '16px 16px 104px' } } >
493+ < h1 style = { { margin : '8px 0 16px' , fontSize : 28 } } > More</ h1 >
494+ < TabBarMoreList value = { value } onChoose = { setValue } >
495+ { tabs }
496+ </ TabBarMoreList >
497+ </ div >
498+ ) : inMore != null ? (
499+ < div style = { { padding : '16px 16px 104px' } } >
500+ < Button
501+ label = "More"
502+ variant = "ghost"
503+ onClick = { ( ) => setValue ( 'more' ) }
504+ />
505+ < h1 style = { { margin : '8px 0 16px' , fontSize : 28 } } > { inMore . label } </ h1 >
506+ </ div >
507+ ) : (
508+ < Screen
509+ title = { SIX . find ( section => section . value === value ) ?. label ?? '' }
510+ reselects = { reselects }
511+ />
512+ ) }
513+ < TabBar
514+ value = { value }
515+ onChange = { setValue }
516+ onReselect = { reselected => {
517+ // More again returns to its list, as iOS pops More's stack.
518+ if ( reselected === 'more' ) {
519+ setValue ( 'more' ) ;
520+ } else {
521+ setReselects ( n => n + 1 ) ;
522+ }
523+ } }
524+ label = "Sections"
525+ data-testid = "tab-bar" >
526+ { tabs }
527+ { hasSearch ? (
528+ < TabBarSearchTab
529+ value = "search"
530+ label = "Search"
531+ icon = { < Icon icon = { MagnifyingGlassIcon } size = "lg" /> }
532+ isActive = { isSearchActive }
533+ onActiveChange = { setIsSearchActive }
534+ automaticallyActivates = { false }
535+ fieldRef = { fieldRef } >
536+ < SearchField
537+ placement = "tab"
538+ label = "Search"
539+ placeholder = "Search"
540+ value = { query }
541+ onChange = { setQuery }
542+ isActive = { isSearchActive }
543+ onActiveChange = { setIsSearchActive }
544+ handleRef = { fieldRef }
545+ />
546+ </ TabBarSearchTab >
547+ ) : null }
548+ </ TabBar >
549+ </ >
550+ ) ;
551+ }
552+
553+ /**
554+ * Six tabs: the bar shows the first four and More, whose list holds the
555+ * rest. A section chosen there opens inside More, which stays selected;
556+ * More again returns to the list.
557+ */
558+ export const MoreTab : Story = {
559+ render : ( ) => < MoreDemo /> ,
560+ } ;
561+
562+ /**
563+ * Six tabs and a search tab: a section opened inside More stays in the
564+ * search tab's leading circle (as More) while searching.
565+ */
566+ export const MoreTabWithSearch : Story = {
567+ render : ( ) => < MoreDemo hasSearch /> ,
568+ } ;
569+
277570/** Five tabs: the items overlap more, as they do on iOS. */
278571export const FiveTabs : Story = {
279572 render : ( ) => (
0 commit comments