Styles
The typed styles contract every family shares, how part styles merge with the defaults, how custom children join a part, and how family authors resolve styles with usePartStyles.
On this page
Every family takes a styles prop: a map from part name to a resolver. A resolver receives the current tokens, variant, size and state, and returns NitroUI view props for that part. Because it reads the state, a custom style still follows dark mode, disabled, pressed, selected and invalid without extra props.
The contract
nitro-ui-sdk/parts exports these types:
import type { SharedViewProps, TextProps } from "nitro-ui-renderer";
import type { ThemeTokens } from "nitro-ui-sdk/theme";
export type StyleContext<Variant extends string, Size extends string, State> = {
tokens: ThemeTokens; // the current scheme's tokens
variant: Variant; // e.g. "primary"
size: Size; // "sm" | "md" | "lg" for most families
state: State; // the family's state, e.g. { disabled, busy }
};
export interface NativePartStyle {
layout?: SharedViewProps["layout"]; // flexbox, padding, size
style?: SharedViewProps["style"]; // background, cornerRadius, opacity, borders
text?: Pick<
TextProps,
"color" | "fontSize" | "fontWeight" | "lineHeight" | "textAlign" | "numberOfLines"
>;
}
export type PartStyles<
Part extends string,
Variant extends string,
Size extends string,
State,
> = Partial<Record<Part, (context: StyleContext<Variant, Size, State>) => NativePartStyle>>;
Each family exports its prop types, so the exact map is ButtonProps["styles"], SwitchProps["styles"] and so on. A part name that does not exist, or a state field the family does not have, fails the type check.
Overriding a part
Your resolver runs after the family’s default for the same part. The results merge one level deep: your layout keys replace the default’s layout keys, and the same for style and text. Anything you do not return keeps its default.
import { Button, type ButtonProps } from "nitro-ui-sdk/button";
// Hoisted: a stable object reference preserves the cache in families using `usePartStyles`.
const pill: ButtonProps["styles"] = {
root: ({ tokens, state }) => ({
layout: { paddingHorizontal: tokens.spacing.xxl },
style: {
cornerRadius: tokens.radius.pill,
opacity: state.disabled ? tokens.opacity.disabled : 1,
},
}),
label: () => ({ text: { fontWeight: "bold" } }),
};
export function Continue({ onPress }: { onPress(): void }) {
return <Button.Root label="Continue" styles={pill} onPress={onPress} />;
}
Define styles outside the component or memoize it. Families that resolve parts with usePartStyles cache each part until tokens, variant, size, a state field or the styles object changes, so passing a fresh object every render invalidates that cache. (Callers that invoke resolvePartStyle directly recompute on each render.)
What each resolver receives
The state fields are listed on every component page under Styles. Common ones:
| Field | Families (examples) | Meaning |
|---|---|---|
disabled |
every interactive family | Actions are refused; draw at opacity.disabled. |
busy |
Button, IconButton | An async press is running. |
checked |
Checkbox, Switch | On or off; Checkbox also has "mixed". |
selected |
Chip, ListGroup rows, Tabs | The item is the current selection. |
value |
Slider, RadioGroup, SegmentedControl, Select | The current value. Slider also has min, max and step. |
open |
Dialog, Menu, Select, Sheet | The presentation is showing. |
focused, invalid |
SecureField and the text editors | Editing focus and validation. |
Custom children in a part
Compound parts are ordinary components, so you can replace one with your own. To keep the family’s styles, read them with the family’s use<Name>Part hook and accept a typed part prop, so the caller can say which slot your component fills:
import { Text } from "nitro-ui-renderer";
import type { PartProps } from "nitro-ui-sdk/parts";
import { Button, useButtonPart, type ButtonPart } from "nitro-ui-sdk/button";
function Price({ amount, ...props }: PartProps<ButtonPart> & { amount: string }) {
const { text, ...view } = useButtonPart(props, "trailing");
return (
<Text {...view} {...text}>
{amount}
</Text>
);
}
<Button.Root label="Buy for 4.99" onPress={buy}>
<Button.Label>Buy</Button.Label>
<Price amount="4.99" />
</Button.Root>;
useButtonPart throws outside Button.Root, so a misplaced part fails loudly in development.
OS-drawn parts
Native hosts own part of their drawing: UIKit draws the switch track and thumb, the slider, the picker wheel and the date picker. Their parts accept the colors and sizes the native view supports (for example a switch’s control part reads text.color as its active track color) and ignore the rest. Each family’s page says which parts are native.
For family authors: usePartStyles
Inside a family, resolve all parts with usePartStyles from nitro-ui-sdk/parts. It memoizes one resolver per context, and each part is computed at most once until the tokens, variant, size, a state field or the styles prop changes.
import { usePartStyles, type PartStyles } from "nitro-ui-sdk/parts";
import { useTokens } from "nitro-ui-sdk/theme";
type Part = "root" | "label";
type State = { disabled: boolean; selected: boolean };
const defaults: PartStyles<Part, "solid" | "soft", "sm" | "md", State> = {
root: ({ tokens, variant, state }) => ({
style: {
background: state.selected ? tokens.colors.accent : variant === "soft" ? tokens.colors.accentSoft : tokens.colors.default,
cornerRadius: tokens.radius.control,
},
}),
label: ({ tokens, state }) => ({
text: { color: state.selected ? tokens.colors.accentForeground : tokens.colors.defaultForeground },
}),
};
function useTagStyles(props: { variant: "solid" | "soft"; size: "sm" | "md"; styles?: typeof defaults }, state: State) {
const tokens = useTokens();
const { style } = usePartStyles(tokens, props.variant, props.size, state, defaults, props.styles);
return { root: style("root"), label: style("label") };
}
state is compared field by field (useShallowStable), so passing a fresh { disabled, selected } object each render does not invalidate the cache.