Skip to content

SizeProvider

Context provider that transmits component size to child components. Used internally by components like Button to pass their size to nested icons.

import { SizeProvider, useSize } from 'asterui'

When using components like Button with the icon prop, the size is automatically propagated to the icon:

import { Button } from 'asterui'
import { StarIcon } from '@aster-ui/icons'
function App() {
return (
<Button size="lg" icon={<StarIcon />}>
Large Button
</Button>
)
}

The StarIcon will automatically receive the lg size from the Button.

Use SizeProvider to create custom components that propagate size to children:

import { SizeProvider, useSize } from 'asterui'
function CustomIconButton({ size = 'md', icon, children }) {
return (
<SizeProvider size={size}>
<button className="custom-button">
{icon}
{children}
</button>
</SizeProvider>
)
}

Use the useSize hook to access the current size context:

import { useSize } from 'asterui'
const sizeMap = {
xs: 12,
sm: 16,
md: 20,
lg: 24,
xl: 28,
}
function CustomIcon() {
const size = useSize() ?? 'md'
const pixels = sizeMap[size]
return (
<svg width={pixels} height={pixels} viewBox="0 0 24 24">
{/* ... */}
</svg>
)
}
PropertyTypeDescription
size'xs' | 'sm' | 'md' | 'lg' | 'xl'Size to provide to child components.
childrenReact.ReactNodeChild components that can consume the size context.
ReturnTypeDescription
sizeSize | undefinedThe current size from the nearest SizeProvider, or undefined if not within a provider.
SizeTypical Pixel Value
xs12px
sm16px
md20px
lg24px
xl28px

The following components use SizeProvider internally to propagate their size:

  • Button: Passes size to the icon prop
  • CopyButton: Passes size to built-in icons

The @aster-ui/icons package is designed to work seamlessly with SizeProvider:

import { Button } from 'asterui'
import { HeartIcon, StarIcon } from '@aster-ui/icons'
function App() {
return (
<>
<Button size="sm" icon={<HeartIcon />}>Small</Button>
<Button size="lg" icon={<StarIcon />}>Large</Button>
</>
)
}

Icons automatically adjust their size based on the parent component’s size without explicit configuration.