Icon pull
Pull an icon beside a label toward the edge, so the padding on its side looks as wide as on the label side. Set it in a recipe with iconPullStart and iconPullEnd, and nudge a single icon with offset.
A button with an icon and a label usually gets the same padding on both sides. On screen the icon side looks wider, because an icon's box is wider than the icon you see: a 24-unit plus only starts drawing 5.25 units in. Pulling the icon toward the edge by a fixed amount makes the two sides look even.
iconPullStart="0"
iconPullStart="1"
import { Flex } from "@isbatak/react-ui/flex"
import { Icon } from "@isbatak/react-ui/icon"
import { Text } from "@isbatak/react-ui/text"
export function IconPullCompareDemo() {
return (
<Flex wrap="wrap" justifyContent="center" gap="8">
<Flex direction="column" alignItems="center" gap="3">
<Flex
alignItems="center"
gap="2"
h="12"
px="5"
borderWidth="1px"
rounded="l2"
textStyle="sm"
fontWeight="medium"
>
<Icon name="plus" size="md" iconPullStart="0" />
New file
</Flex>
<Flex
alignItems="center"
gap="2"
h="12"
px="5"
borderWidth="1px"
rounded="l2"
textStyle="sm"
fontWeight="medium"
>
Continue
<Icon name="arrow-right" size="md" iconPullEnd="0" />
</Flex>
<Text textStyle="xs" fontFamily="mono" color="fg.muted">
iconPullStart="0"
</Text>
</Flex>
<Flex direction="column" alignItems="center" gap="3">
<Flex
alignItems="center"
gap="2"
h="12"
px="5"
borderWidth="1px"
rounded="l2"
textStyle="sm"
fontWeight="medium"
>
<Icon name="plus" size="md" iconPullStart="1" />
New file
</Flex>
<Flex
alignItems="center"
gap="2"
h="12"
px="5"
borderWidth="1px"
rounded="l2"
textStyle="sm"
fontWeight="medium"
>
Continue
<Icon name="arrow-right" size="md" iconPullEnd="1" />
</Flex>
<Text textStyle="xs" fontFamily="mono" color="fg.muted">
iconPullStart="1"
</Text>
</Flex>
</Flex>
)
}Three utilities ship with @isbatak/panda-ds:
| Utility | Takes | Sets |
|---|---|---|
iconPullStart | spacing token | A negative margin-inline-start, plus --icon-offset |
iconPullEnd | spacing token | A negative margin-inline-end, plus --icon-offset |
iconPullOffset | spacing token | --icon-offset, the extra pull for a single icon |
In a recipe
Put the pull on the icon slots with the _iconStart and _iconEnd conditions, which match StartIcon and EndIcon.
The value is positive; the utility makes the margin negative.
size: {
sm: {
_iconStart: { iconPullStart: "0.5" },
_iconEnd: { iconPullEnd: "0.5" },
},
md: {
_iconStart: { iconPullStart: "1" },
_iconEnd: { iconPullEnd: "1" },
},
}Button, Badge, Timer and Kbd already do this, so an icon in StartIcon or EndIcon is pulled without extra styles. Kbd
uses "0": its keys are too tight to pull, but a single icon can still take an offset.
Keep the pull the same for every icon at a size. Labels in a stack of buttons then share one edge, whatever icon sits before them.
One icon at a time
Some icons carry more air than others. To pull a single icon further, to its ink, pass offset to StartIcon or
EndIcon as a CSS length. It's added to the recipe's pull, and a negative length pulls less. Drag the slider.
margin-inline-start: calc((spacing.1 + 0px) * -1)
import { Flex } from "@isbatak/react-ui/flex"
import { Icon } from "@isbatak/react-ui/icon"
import { Slider } from "@isbatak/react-ui/slider"
import { Span } from "@isbatak/react-ui/span"
import { Text } from "@isbatak/react-ui/text"
import { type CSSProperties, useState } from "react"
export function IconPullOffsetDemo() {
const [offset, setOffset] = useState(0)
const style = { "--icon-offset": `${offset}px` } as CSSProperties
return (
<Flex direction="column" gap="6" w="full" maxW="sm">
<Flex justifyContent="center">
<Flex
alignItems="center"
gap="2"
h="16"
px="6"
borderWidth="1px"
rounded="l2"
textStyle="md"
fontWeight="medium"
>
<Icon name="plus" size="lg" iconPullStart="1" style={style} />
New file
</Flex>
</Flex>
<Slider.Root
value={[offset]}
min={-4}
max={4}
step={0.25}
onValueChange={(details) => setOffset(details.value[0] ?? 0)}
>
<Flex justifyContent="space-between" textStyle="sm">
<Slider.Label>--icon-offset</Slider.Label>
<Span fontFamily="mono" color="fg.muted">
{offset}px
</Span>
</Flex>
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
<Text textStyle="xs" fontFamily="mono" color="fg.muted" textAlign="center">
margin-inline-start: calc((spacing.1 + {offset}px) * -1)
</Text>
</Flex>
)
}<Button>
<StartIcon asChild offset="0.4px">
<Icon name="plus" />
</StartIcon>
New file
</Button>Use em when the icon scales with the text, as in Kbd, so one value fits every size.
<Kbd>
<StartIcon asChild offset="0.18em">
<Icon name="command" />
</StartIcon>
K
</Kbd>offset sets --icon-offset on the icon. iconPullOffset sets the same variable from a style, for a recipe that wants
a default extra pull. The prop wins, because it's set on the element itself.
_iconStart: {
iconPullStart: "1",
iconPullOffset: "0.5",
},See Optical alignment for button icons for how to measure an icon's ink.
Outside a recipe
The utilities are plain style props, so they work on any element. Put them on the icon itself:
<Flex alignItems="center" gap="2" h="12" px="5" borderWidth="1px" rounded="l2">
<Icon name="plus" size="md" iconPullStart="1" />
New file
</Flex>iconPullOffset sets the extra pull the same way:
<Icon name="plus" size="md" iconPullStart="1" iconPullOffset="0.5" />How it works
iconPullStart: "1" writes one margin that adds the token and the offset, then flips the sign:
.icon-pull-start_1 {
margin-inline-start: calc((var(--spacing-1) + var(--icon-offset, 0px)) * -1);
}--icon-offsetfalls back to0px, so without an offset the icon is pulled by the token alone."0"drops the token and keeps only the offset, sincecalc(0 + 0px)mixes a unitless zero with a length and is invalid.- Recipes sit in Panda's
recipeslayer andcss()inutilities, so a class on the icon overrides the recipe's pull.
FAQ
A plain margin replaces the pull instead of adding to it. A single icon would then need the recipe's value for its size plus its own, and the fix breaks when the size changes. The utilities keep the recipe's pull and the icon's offset in separate places and add them.
Yes. The margins are logical, so in RTL iconPullStart pulls the icon toward the right edge.