Skip to content

Instantly share code, notes, and snippets.

@notzheng
Created April 19, 2023 06:58
Show Gist options
  • Save notzheng/644b31fca1733e0c1cfcc72f4ff13e73 to your computer and use it in GitHub Desktop.
Save notzheng/644b31fca1733e0c1cfcc72f4ff13e73 to your computer and use it in GitHub Desktop.
`useStyles` for Ant Design V5
import createStyles from '@/styles/create-styles';
export const useStyles = createStyles((token) => ({
container: {
backgroundColor: token.colorBgBase,
borderRadius: token.borderRadius,
border: `1px solid ${token.colorBorderSecondary}`,
color: token.colorText,
},
header: {
display: 'flex',
alignItems: 'center',
padding: token.padding,
},
info: {
flexGrow: 1,
},
title: {
fontSize: token.fontSize,
},
description: {
fontSize: token.fontSizeSM,
color: token.colorTextDescription,
marginTop: token.marginXS,
},
toggle: {},
body: {
padding: token.padding,
borderTop: `1px dashed ${token.colorBorderSecondary}`,
},
}));
import { GlobalToken, theme } from 'antd';
import clsx from 'clsx';
import { css, CSSInterpolation } from '@emotion/css';
type CssValues = Record<string, CSSInterpolation>;
export default function createStyles<T extends CssValues = CssValues, P = void>(
getCssValues: (token: GlobalToken, params: P) => T,
) {
function useStyles(params: P) {
const { token } = theme.useToken();
const cssValues = getCssValues(token, params);
const classes = Object.fromEntries(
Object.entries(cssValues).map(([name, value]) => {
return [name, css(value)];
}),
) as { [key in keyof T]: string; };
return {
classes,
cx: clsx,
} as const;
}
return useStyles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment