@cleartrip/ct-design-style-manager
Cleartrip Design System的造型公用设施集合. 提供将样式定义转换为 情感 class name for web,同时保持与 React 原住民风格形状的完全相容.
安装
pnpm add @cleartrip/ct-design-style-manager
API 参考
makeStyles
A 类 无挂起 工厂的反动第一风格.
接受接收当前主题并返回 a 的工厂函数 NamedStyles 对象( 使用 ViewStyle, (中文(简体) ), TextStyle,或 ImageStyle 属性)。 风格经过验证,预处理(RN → 网页转换,Shabow/textShadow扩展),每个键被转换为情感类名称字符串.
import { makeStyles } from '@cleartrip/ct-design-style-manager';
const styles = makeStyles((theme) => ({
container: {
flexDirection: 'row',
backgroundColor: theme.color.neutral_10,
padding: theme.spacing.s4,
},
title: {
fontSize: theme.font.size.large,
color: theme.color.neutral_90,
},
}));
// styles.container → 'css-<hash>'
// styles.title → 'css-<hash>'
签署
function makeStyles<T extends NamedStyles<T>>(
styleFactory: (theme: Theme) => T | NamedStyles<T> | NamedStyles<unknown>,
customTheme?: Theme,
): T;
使用
customTheme以覆盖活动主题(例如测试或服务器侧渲染).
useStyles
这个 钩子 对应单位 makeStyles用途 useTheme() 以被动方式读取活动主题,并通过 useStableMemo 所以样式只有在 deps 变化.
import { useStyles } from '@cleartrip/ct-design-style-manager';
function MyComponent() {
const styles = useStyles(
(theme) => ({
container: { padding: theme.spacing.s4 },
label: { color: theme.color.neutral_90 },
}),
[],
);
return <div className={styles.container}>…</div>;
}
签署
function useStyles<T extends NamedStyles<T>>(
styleFactory: (theme: Theme) => T | NamedStyles<T> | NamedStyles<unknown>,
deps: DependencyList,
): T;
makeWebStyles 新设
A 类 无挂起 工厂 仅网络 使用标准反应的样式 CSSProperties。 。 。 .
与 makeStyles,不应用反应-内在预处理——数值直接传递给情感. 当您正在编写仅网络组件时使用此选项,并想要使用标准的 CSS 属性名称(例如. backgroundColor, (中文(简体) ), fontSize, (中文(简体) ), display) (中文(简体) ).
import { makeWebStyles } from '@cleartrip/ct-design-style-manager';
const styles = makeWebStyles((theme) => ({
container: {
display: 'flex',
gap: theme.spacing.s4,
backgroundColor: theme.color.neutral_10,
},
title: {
color: theme.color.neutral_90,
fontSize: 16,
fontWeight: 600,
},
}));
// styles.container → 'css-<hash>'
// styles.title → 'css-<hash>'
签署
function makeWebStyles<T extends NamedWebStyles<T>>(
styleFactory: (theme: Theme) => T | NamedWebStyles<T>,
customTheme?: Theme,
): { [P in keyof T]: string };
地点 NamedWebStyles<T> = { [P in keyof T]: CSSProperties }。 。 。 .
使用
customTheme以覆盖活动主题(例如在测试或SSSR中).
useWebStyles 新设
这个 钩子 对应单位 makeWebStyles。通过 useTheme() 和回忆通过 useStableMemo。 。 。 .
import { useWebStyles } from '@cleartrip/ct-design-style-manager';
function MyComponent() {
const styles = useWebStyles(
(theme) => ({
wrapper: {
display: 'flex',
flexDirection: 'column',
gap: theme.spacing.s4,
},
heading: {
color: theme.color.neutral_90,
fontSize: 20,
},
}),
[],
);
return (
<div className={styles.wrapper}>
<h1 className={styles.heading}>Hello</h1>
</div>
);
}
签署
function useWebStyles<T extends NamedWebStyles<T>>(
styleFactory: (theme: Theme) => T | NamedWebStyles<T>,
deps: DependencyList,
): { [P in keyof T]: string };
useWebMergeStyles (折旧)
将一系列风格来源——现存的情感类名字符串或原始风格对象——合并为单一情感类字符串. 仅用于终端/叶片组件.
import { useWebMergeStyles } from '@cleartrip/ct-design-style-manager';
const className = useWebMergeStyles([styles.base, isActive && styles.active, propClassName], [isActive, propClassName]);
签署
const useWebMergeStyles: <T extends Styles | string>(styles: T[], deps: DependencyList) => string;
useNativeMergeStyles (折旧)
合并本地反应数组 StyleProp 数值。 仅用于终端/叶生组件.
import { useNativeMergeStyles } from '@cleartrip/ct-design-style-manager';
const merged = useNativeMergeStyles([baseStyle, conditionalStyle], [dep]);
签署
function useNativeMergeStyles<T extends ViewStyle | ImageStyle | TextStyle>(
styles: StyleProp<T>[],
deps: DependencyList,
): StyleProp<T>;
gs — 全局布局样式
模块加载时间( 没有主题依赖性) 已创建的全局布局工具类名称 。 根据需要导入特定密钥 .
import { gs } from '@cleartrip/ct-design-style-manager';
<div className={gs.flexRow}>…</div>
选择正确的 API
| 假设 | 使用 |
|---|---|
| 使用 RN 风格属性的跨平台组件( web + RN) | makeStyles 编号 : useStyles |
| 使用标准 CSS 属性的仅网络组件 | makeWebStyles 编号 : useWebStyles |
| 在运行时合并多个类名称/ 样式对象( web) | useWebMergeStyles |
| 在运行时合并多个样式对象( 本地) | useNativeMergeStyles |
| 静态布局公用事业(不需要主题) | gs |
关键差异 : makeStyles 对 makeWebStyles
makeStyles | makeWebStyles | |
|---|---|---|
| 输入属性类型 | ViewStyle 编号 : TextStyle 编号 : ImageStyle (韩文) | CSSProperties (反应/网络) |
| 预处理 | RN – 网络转换,阴影扩展 | 没有——直接传给情感 |
| 验证 | QQ RN 样式验证 | 未应用 |
| 平台 | 跨平台 | 仅网络 |
| 产出 | 情感类名称字符串( 键形状与输入相同) | 情感类名称字符串( 键形状与输入相同) |