11// A bar's planned layout, headless: the bar's UI owns its plans, how they are measured and its policy; this owns the
22// measurement cache, the observation, the settled marker, the interaction lock and focus retention.
33
4- import { useEffect , useLayoutEffect , useRef , useState } from 'react'
4+ import { useEffect , useLayoutEffect , useRef } from 'react'
5+ import { useMachine } from '@githubnext/projections-machine/react'
56import type { RefObject } from 'react'
67import { flushSync } from 'react-dom'
78import { chooseFitPlan , toolbarHysteresis } from './fit-plan/choose.js'
89import { fitWidth } from './fit-plan/fit-width.js'
910import { useFocusRetention } from './fit-plan/focus.js'
11+ import { fitPlanLifecycle } from './fit-plan/lifecycle.js'
1012
1113export { chooseFitPlan , toolbarHysteresis } from './fit-plan/choose.js'
1214export type { FitChoice , FitHysteresis } from './fit-plan/choose.js'
@@ -65,17 +67,22 @@ export function useFitPlan<T>(
6567 const index = list . findIndex ( item => sign ( item ) === sign ( plan ) )
6668 return index < 0 ? 0 : index
6769 } )
68- const [ state , setState ] = useState < { plan : T ; width : number ; tick : number } > ( ( ) => ( { plan : plans [ 0 ] ! , width : 0 , tick : 0 } ) )
69- const memo = useRef ( {
70- key,
71- needs : new Map < string , number > ( ) ,
72- ceilings : new Map < string , number > ( ) ,
73- settled : plans [ 0 ] ! ,
74- locked : false ,
75- steps : 0 ,
76- burst : '' ,
70+ const memo = useRef ( { key, needs : new Map < string , number > ( ) , ceilings : new Map < string , number > ( ) , steps : 0 , burst : '' } )
71+ const [ state , send , actor ] = useMachine ( fitPlanLifecycle , {
72+ state : { kind : 'planning' , plan : plans [ 0 ] ! , settled : plans [ 0 ] ! , width : 0 , run : 0 , hand : 'free' , frame : false } ,
73+ activities : {
74+ // Replanning inside the observer's callback would resize observed elements mid-delivery (a ResizeObserver loop),
75+ // so it waits for the next frame, and renders synchronously there, before that frame's layout and paint.
76+ frame : ( _activity , { send} ) => {
77+ const handle = requestAnimationFrame ( ( ) => {
78+ const element = bar . current
79+ if ( element ) flushSync ( ( ) => send ( { type : 'frame' , width : Math . round ( element . getBoundingClientRect ( ) . width ) } ) )
80+ } )
81+ return ( ) => cancelAnimationFrame ( handle )
82+ } ,
83+ } ,
7784 } )
78- const index = find ( plans , state . plan )
85+ const index = find ( plans , state . plan as T )
7986 const plan = plans [ index ] !
8087 const { lock, ceiling, measure = fitWidth , hysteresis = toolbarHysteresis } = options
8188 const latest = useRef ( { sign, find, lock, ceiling, measure, hysteresis} )
@@ -94,18 +101,16 @@ export function useFitPlan<T>(
94101 const width = element . getBoundingClientRect ( ) . width
95102 if ( Math . round ( width ) !== state . width ) {
96103 // What a plan needs may depend on the width (say, a slider's minimum): render with it before measuring.
97- delete element . dataset . fitSettled
98- setState ( previous => ( { ...previous , width : Math . round ( width ) } ) )
104+ send ( { type : 'resized' , width : Math . round ( width ) } )
99105 return
100106 }
101107 m . needs . set ( sign ( plan ) , Math . ceil ( measure ( element , plan ) ) )
102108 if ( ceiling ) m . ceilings . set ( sign ( plan ) , ceiling ( element , plan ) )
103109 const lock = lockOf ?.( element , plan )
104- m . locked = Boolean ( lock )
105110 const target = chooseFitPlan ( {
106111 plans,
107112 width,
108- settled : find ( plans , m . settled ) ,
113+ settled : find ( plans , actor . getState ( ) . settled as T ) ,
109114 need : candidate => m . needs . get ( sign ( candidate ) ) ,
110115 ceiling : candidate => m . ceilings . get ( sign ( candidate ) ) ,
111116 hysteresis : typeof hysteresis === 'number' ? hysteresis : ( from , to ) => hysteresis ( from , to , element ) ,
@@ -122,41 +127,33 @@ export function useFitPlan<T>(
122127 }
123128 const known = m . needs . has ( sign ( plans [ target ] ! ) )
124129 const switches = known ? ++ m . steps : m . steps
125- if ( target !== index && ( ! known || switches <= 4 || ( ! fits ( plan ) && switches <= 4 + plans . length ) ) ) {
126- delete element . dataset . fitSettled
127- setState ( previous => ( { ...previous , plan : plans [ target ] ! } ) )
128- } else {
129- m . settled = plan
130- element . dataset . fitSettled = String ( Math . round ( width ) )
131- }
132- } , [ bar , plans , plan , index , key , state . width , state . tick ] )
130+ if ( target !== index && ( ! known || switches <= 4 || ( ! fits ( plan ) && switches <= 4 + plans . length ) ) )
131+ send ( { type : 'tried' , plan : plans [ target ] ! } )
132+ else send ( { type : 'fitted' , plan, locked : Boolean ( lock ) } )
133+ } , [ bar , send , actor , plans , plan , index , key , state . width , state . run ] )
134+
135+ // Once a plan settles, the bar says at what width, so a test can wait for it.
136+ useLayoutEffect ( ( ) => {
137+ const element = bar . current
138+ if ( ! element ) return
139+ if ( state . kind === 'planning' ) delete element . dataset . fitSettled
140+ else element . dataset . fitSettled = String ( state . width )
141+ } , [ bar , state . kind , state . width ] )
133142
134143 // Replan when the bar or one of its regions changes size.
135144 const observer = useRef < ResizeObserver | undefined > ( undefined )
136145 useLayoutEffect ( ( ) => {
137146 const element = bar . current
138147 if ( ! element ) return
139- // Replanning inside the observer's callback would resize observed elements mid-delivery (a ResizeObserver loop),
140- // so it waits for the next frame, and renders synchronously there, before that frame's layout and paint.
141- let frame = 0
142- const resize = new ResizeObserver ( ( ) => {
143- if ( frame ) return
144- frame = requestAnimationFrame ( ( ) => {
145- frame = 0
146- flushSync ( ( ) =>
147- setState ( previous => ( { ...previous , width : Math . round ( element . getBoundingClientRect ( ) . width ) , tick : previous . tick + 1 } ) ) ,
148- )
149- } )
150- } )
148+ const resize = new ResizeObserver ( ( ) => send ( { type : 'observed' } ) )
151149 observer . current = resize
152150 resize . observe ( element )
153151 return ( ) => {
154- cancelAnimationFrame ( frame )
155152 resize . disconnect ( )
156153 observer . current = undefined
157154 observed . current = new Set ( )
158155 }
159- } , [ bar ] )
156+ } , [ bar , send ] )
160157 const observed = useRef ( new Set < Element > ( ) )
161158 useLayoutEffect ( ( ) => {
162159 const resize = observer . current
@@ -168,47 +165,26 @@ export function useFitPlan<T>(
168165 observed . current = regions
169166 } )
170167
171- // Once focus leaves a control kept in use, plan again, but not between a pointer's press and its click: trying
172- // other plans then would remount the control being clicked (WebKit moves focus away on press).
168+ // Focus moving and a pointer's press and release, for the lock (`fitPlanLifecycle`).
173169 useEffect ( ( ) => {
174170 const document = bar . current ?. ownerDocument
175171 if ( ! document ) return
176- let timer : ReturnType < typeof setTimeout > | undefined
177- let pressed = false
178- let pending = false
179- const replan = ( ) => {
180- clearTimeout ( timer )
181- timer = setTimeout ( ( ) => setState ( previous => ( { ...previous , tick : previous . tick + 1 } ) ) , 0 )
182- }
183- const moved = ( ) => {
184- if ( ! memo . current . locked ) return
185- if ( pressed ) pending = true
186- else replan ( )
187- }
188- const press = ( ) => {
189- pressed = true
190- }
191- const release = ( ) => {
192- pressed = false
193- if ( pending ) {
194- pending = false
195- replan ( )
196- }
197- }
172+ const moved = ( ) => send ( { type : 'moved' } )
173+ const press = ( ) => send ( { type : 'pressed' } )
174+ const release = ( ) => send ( { type : 'released' } )
198175 document . addEventListener ( 'focusin' , moved )
199176 document . addEventListener ( 'focusout' , moved )
200177 document . addEventListener ( 'pointerdown' , press , true )
201178 document . addEventListener ( 'pointerup' , release , true )
202179 document . addEventListener ( 'pointercancel' , release , true )
203180 return ( ) => {
204- clearTimeout ( timer )
205181 document . removeEventListener ( 'focusin' , moved )
206182 document . removeEventListener ( 'focusout' , moved )
207183 document . removeEventListener ( 'pointerdown' , press , true )
208184 document . removeEventListener ( 'pointerup' , release , true )
209185 document . removeEventListener ( 'pointercancel' , release , true )
210186 }
211- } , [ bar ] )
187+ } , [ bar , send ] )
212188
213189 useFocusRetention ( bar , options . focusFallbacks )
214190
0 commit comments