-
Notifications
You must be signed in to change notification settings - Fork 9
feat(responsive): add useContainerWidth hook #1144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JeanMarcMilletScality
wants to merge
4
commits into
development/1.0
Choose a base branch
from
improvement/responsive-use-container-width-hook
base: development/1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−0
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f097686
feat(responsive): add useContainerWidth hook
JeanMarcMilletScality 1732d8c
test(responsive): cover useContainerWidth behaviour
JeanMarcMilletScality 4eced25
test(responsive): clarify useContainerWidth test names
JeanMarcMilletScality c736d3f
fix(responsive): report border-box width consistently
JeanMarcMilletScality File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,100 @@ | ||
| import { useCallback, useRef, useState } from 'react'; | ||
|
|
||
| /** | ||
| * Container width (in px) below which a screen should switch to its compact | ||
| * layout. The trigger is the *container* width, not the viewport, so screens | ||
| * stay usable when a host side-panel shrinks the available space without | ||
| * changing the viewport width. | ||
| */ | ||
| export const NARROW_BREAKPOINT_PX = 640; | ||
|
|
||
| /** | ||
| * Tables carry several columns and a search/action toolbar, so they feel | ||
| * cramped at a wider width than a single-column detail form does. List screens | ||
| * pass this higher breakpoint to drop secondary columns / shrink the toolbar | ||
| * earlier than detail panels switch to their compact layout. | ||
| */ | ||
| export const TABLE_NARROW_BREAKPOINT_PX = 820; | ||
|
|
||
| export type UseContainerWidthOptions = { | ||
| /** | ||
| * Hysteresis band (px). Once narrow, the container must grow back to | ||
| * `breakpoint + hysteresis` before it is considered wide again. Prevents | ||
| * flicker when the container is dragged to rest right on a breakpoint. | ||
| * Defaults to 0 (no band). | ||
| */ | ||
| hysteresis?: number; | ||
| }; | ||
|
|
||
| export type UseContainerWidthResult<T extends HTMLElement> = { | ||
| /** Callback ref — attach to the element whose width should drive the layout. */ | ||
| ref: (node: T | null) => void; | ||
| /** Latest measured content-box width in px, or `null` before first measure. */ | ||
| width: number | null; | ||
| /** True when `width` is below the hook's `breakpoint`. Wide-first until measured. */ | ||
| isNarrow: boolean; | ||
| /** True when `width` is below `px`. Lets one container drive several tiers. */ | ||
| isNarrowerThan: (px: number) => boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Observes the width of the element the returned `ref` is attached to. | ||
| * | ||
| * Width starts as `null` (not yet measured) so the first paint defaults to the | ||
| * wide layout; the observer then measures and flips to narrow when needed, | ||
| * avoiding a visible flash on full-width screens. | ||
| * | ||
| * `ref` is a *callback* ref (not a ref object) so the observer attaches whenever | ||
| * the node mounts — including when the measured element only appears after an | ||
| * async loading state, where a `useEffect([])` would have run too early (while | ||
| * the node was still null) and never observed it. | ||
| */ | ||
| export function useContainerWidth<T extends HTMLElement = HTMLDivElement>( | ||
| breakpoint: number = NARROW_BREAKPOINT_PX, | ||
| options: UseContainerWidthOptions = {}, | ||
| ): UseContainerWidthResult<T> { | ||
| const { hysteresis = 0 } = options; | ||
| const [width, setWidth] = useState<number | null>(null); | ||
| const observerRef = useRef<ResizeObserver | null>(null); | ||
| const isNarrowRef = useRef(false); | ||
|
|
||
| const ref = useCallback((node: T | null) => { | ||
| if (observerRef.current) { | ||
| observerRef.current.disconnect(); | ||
| observerRef.current = null; | ||
| } | ||
| if (!node || typeof ResizeObserver === 'undefined') { | ||
| return; | ||
| } | ||
| const observer = new ResizeObserver((entries) => { | ||
| const observedWidth = entries[0]?.contentRect.width; | ||
| if (typeof observedWidth === 'number') { | ||
| const rounded = Math.round(observedWidth); | ||
| setWidth((prev) => (prev === rounded ? prev : rounded)); | ||
| } | ||
| }); | ||
| observer.observe(node); | ||
| observerRef.current = observer; | ||
| setWidth(Math.round(node.getBoundingClientRect().width)); | ||
| }, []); | ||
|
|
||
| // Hysteresis: once narrow, require width >= breakpoint + hysteresis to leave | ||
| // narrow; once wide, require width < breakpoint to enter narrow. With | ||
| // hysteresis = 0 this is a plain `width < breakpoint`. | ||
| let isNarrow = isNarrowRef.current; | ||
| if (width !== null) { | ||
| if (isNarrow) { | ||
| if (width >= breakpoint + hysteresis) isNarrow = false; | ||
| } else if (width < breakpoint) { | ||
| isNarrow = true; | ||
| } | ||
| isNarrowRef.current = isNarrow; | ||
| } | ||
|
|
||
| const isNarrowerThan = useCallback( | ||
| (px: number) => width !== null && width < px, | ||
| [width], | ||
| ); | ||
|
|
||
| return { ref, width, isNarrow, isNarrowerThan }; | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.