Access component themes with useComponentTheme
mainIf you are building a custom component and want it to participate in the Ink UI theming system, use the useComponentTheme hook. This allows your component to read styles and configuration defined in the current ThemeProvider.
import React, {render, Text, type TextProps} from 'ink';
import {
ThemeProvider,
defaultTheme,
extendTheme,
useComponentTheme,
type ComponentTheme,
} from '@inkjs/ui';
const customLabelTheme = {
styles: {
label: (): TextProps => ({
color: 'green',
}),
},
} satisfies ComponentTheme;
type CustomLabelTheme = typeof customLabelTheme;
const customTheme = extendTheme(defaultTheme, {
components: {
CustomLabel: customLabelTheme,
},
});
function CustomLabel() {
const {styles} = useComponentTheme<CustomLabelTheme>('CustomLabel');
return <Text {...styles.label()}>Hello world</Text>;
}
function Example() {
return (
<ThemeProvider theme={customTheme}>
<CustomLabel />
</ThemeProvider>
);
}
render(<Example />);