Skip to main content

use_page_size

Read the page size and re-render the component when it changes.

Call it inside a component function. Without arguments it returns the page's Size and re-renders the component on every resize. With select, it returns select(size) and re-renders only when that value changes, so a layout that only depends on a breakpoint re-renders only when the page crosses it.

Unlike on_resize, which holds one handler, any number of components can use this hook at once.

Parameters:

  • select (Callable[[Size], Any] | None, default: None) - A function that maps the page size to the value the component needs, such as lambda size: size.width < 600.

Returns:

  • Any - The page size, or select(size) when select is given.
Example
@ft.component
def Shell(body: ft.Control):
compact = ft.use_page_size(lambda size: size.width < 600)
if compact:
return ft.Column([body, NavBar()], expand=True)
return ft.Row([NavRail(), body], expand=True)