Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pr/MaxArtemov/134
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Nov 8, 2024
2 parents 9640297 + eabc24e commit 93ee282
Show file tree
Hide file tree
Showing 49 changed files with 2,573 additions and 469 deletions.
9 changes: 8 additions & 1 deletion .fatherrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ import { defineConfig } from 'father';

export default defineConfig({
plugins: ['@rc-component/father-plugin'],
});
umd: {
name: 'antdCssinjs',
externals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
});
118 changes: 5 additions & 113 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,114 +1,6 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

name: ✅ test
on: [push, pull_request]
jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master

- uses: actions/setup-node@v1
with:
node-version: '12'

- name: cache package-lock.json
uses: actions/cache@v2
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: create package-lock.json
run: npm i --package-lock-only

- name: hack for singe file
run: |
if [ ! -d "package-temp-dir" ]; then
mkdir package-temp-dir
fi
cp package-lock.json package-temp-dir
- name: cache node_modules
id: node_modules_cache_id
uses: actions/cache@v2
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}

- name: install
if: steps.node_modules_cache_id.outputs.cache-hit != 'true'
run: npm ci

lint:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master

- name: restore cache from package-lock.json
uses: actions/cache@v2
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: restore cache from node_modules
uses: actions/cache@v2
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}

- name: lint
run: npm run lint

needs: setup

compile:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master

- name: restore cache from package-lock.json
uses: actions/cache@v2
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: restore cache from node_modules
uses: actions/cache@v2
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}

- name: compile
run: npm run compile

needs: setup

coverage:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master

- name: restore cache from package-lock.json
uses: actions/cache@v2
with:
path: package-temp-dir
key: lock-${{ github.sha }}

- name: restore cache from node_modules
uses: actions/cache@v2
with:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }}

- name: coverage
run: npm run coverage && bash <(curl -s https://codecov.io/bash)

needs: setup
test:
uses: react-component/rc-test/.github/workflows/test.yml@main
secrets: inherit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ assets/**/*.css
build
lib
es
dist
yarn.lock
package-lock.json
coverage/
Expand All @@ -41,3 +42,5 @@ coverage/
.dumi/tmp
.dumi/tmp-production
.dumi/tmp-test

bun.lockb
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ Create theme object. When same algorithm provided, it will return same object.

Since `@ant-design/cssinjs` use strong constraints for cache hit performance, we recommend to view demo `basic.tsx` for usage and `animation.tsx` for animation usage.

### extractStyle

Extracts the styles from the cache and returns them as a string.

#### Parameters

- `cache` (Cache): The cache instance containing the styles.
- `options` (object | boolean, optional): Options for extracting the styles.
- `plain` (boolean, optional): If true, the styles will be returned in plain format. Default is false.
- `types` (string | string[], optional): The types of styles to extract. Default is ['style', 'token', 'cssVar'].

#### Returns

- (string): The extracted styles as a string.

#### Example

```typescript
import { extractStyle, createCache } from '@ant-design/cssinjs';

// 创建并填充缓存
const cache = createCache();
// 注意:在实际使用中,缓存通常会在渲染组件时自动填充

// 提取样式
const styles = extractStyle(cache, { plain: true, types: ['style', 'token'] });

// 使用提取的样式
const styleElement = document.createElement('style');
styleElement.innerHTML = styles;
document.head.appendChild(styleElement);
```

## Transform

When you need transform CSSObject before inject style. You can use `transformers` to handle this:
Expand Down
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[install]
peer = false
8 changes: 8 additions & 0 deletions docs/demo/css-var.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: CSS Variables
nav:
title: Demo
path: /demo
---

<code src="../examples/css-var.tsx"></code>
11 changes: 6 additions & 5 deletions docs/examples/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CSSInterpolation, CSSObject } from '@ant-design/cssinjs';
import { useStyleRegister } from '@ant-design/cssinjs';
import { unit, useStyleRegister } from '@ant-design/cssinjs';
import classNames from 'classnames';
import React from 'react';
import type { DerivativeToken } from './theme';
Expand All @@ -14,6 +14,7 @@ const genSharedButtonStyle = (
borderColor: token.borderColor,
borderWidth: token.borderWidth,
borderRadius: token.borderRadius,
lineHeight: token.lineHeight,

cursor: 'pointer',

Expand Down Expand Up @@ -65,7 +66,7 @@ const genPrimaryButtonStyle = (
): CSSInterpolation =>
genSolidButtonStyle(prefixCls, token, () => ({
backgroundColor: token.primaryColor,
border: `${token.borderWidth}px solid ${token.primaryColor}`,
border: `${unit(token.borderWidth)} solid ${token.primaryColor}`,
color: token.reverseTextColor,

'&:hover': {
Expand All @@ -83,7 +84,7 @@ const genGhostButtonStyle = (
[`.${prefixCls}`]: {
backgroundColor: 'transparent',
color: token.primaryColor,
border: `${token.borderWidth}px solid ${token.primaryColor}`,
border: `${unit(token.borderWidth)} solid ${token.primaryColor}`,

'&:hover': {
borderColor: token.primaryColor,
Expand All @@ -102,7 +103,7 @@ const Button = ({ className, type, ...restProps }: ButtonProps) => {
const prefixCls = 'ant-btn';

// 【自定义】制造样式
const [theme, token, hashId] = useToken();
const [theme, token, hashId, cssVarKey] = useToken();

// default 添加默认样式选择器后可以省很多冲突解决问题
const defaultCls = `${prefixCls}-default`;
Expand All @@ -129,7 +130,7 @@ const Button = ({ className, type, ...restProps }: ButtonProps) => {

return wrapSSR(
<button
className={classNames(prefixCls, typeCls, hashId, className)}
className={classNames(prefixCls, typeCls, hashId, className, cssVarKey)}
{...restProps}
/>,
);
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/components/Spin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Spin = ({ className, ...restProps }: SpinProps) => {
// 全局注册,内部会做缓存优化
const wrapSSR = useStyleRegister(
{ theme, token, hashId, path: [prefixCls] },
() => [genSpinStyle(prefixCls, token, hashId)],
() => [genSpinStyle(prefixCls, token)],
);

return wrapSSR(
Expand Down
36 changes: 30 additions & 6 deletions docs/examples/components/theme.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { TinyColor } from '@ctrl/tinycolor';
import type { CSSObject, Theme } from '@ant-design/cssinjs';
import { createTheme, useCacheToken } from '@ant-design/cssinjs';
import { TinyColor } from '@ctrl/tinycolor';
import React from 'react';

export type GetStyle = (prefixCls: string, token: DerivativeToken) => CSSObject;

Expand All @@ -15,6 +15,9 @@ export interface DesignToken {
borderRadius: number;
borderColor: string;
borderWidth: number;

lineHeight: number;
lineHeightBase: number;
}

export interface DerivativeToken extends DesignToken {
Expand All @@ -31,6 +34,9 @@ const defaultDesignToken: DesignToken = {
borderRadius: 2,
borderColor: 'black',
borderWidth: 1,

lineHeight: 1.5,
lineHeightBase: 1.5,
};

// 模拟推导过程
Expand All @@ -48,21 +54,39 @@ export const ThemeContext = React.createContext(createTheme(derivative));
export const DesignTokenContext = React.createContext<{
token?: Partial<DesignToken>;
hashed?: string | boolean;
cssVar?: {
key: string;
};
}>({
token: defaultDesignToken,
});

export function useToken(): [Theme<any, any>, DerivativeToken, string] {
const { token: rootDesignToken = {}, hashed } =
React.useContext(DesignTokenContext);
export function useToken(): [
Theme<any, any>,
DerivativeToken,
string,
string | undefined,
] {
const {
token: rootDesignToken = {},
hashed,
cssVar,
} = React.useContext(DesignTokenContext);
const theme = React.useContext(ThemeContext);

const [token, hashId] = useCacheToken<DerivativeToken, DesignToken>(
theme,
[defaultDesignToken, rootDesignToken],
{
salt: typeof hashed === 'string' ? hashed : '',
cssVar: cssVar && {
prefix: 'rc',
key: cssVar.key,
unitless: {
lineHeight: true,
},
},
},
);
return [theme, token, hashed ? hashId : ''];
return [theme, token, hashed ? hashId : '', cssVar?.key];
}
52 changes: 52 additions & 0 deletions docs/examples/css-var.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import './basic.less';
import Button from './components/Button';
import { DesignTokenContext } from './components/theme';

export default function App() {
const [show, setShow] = React.useState(true);

const [, forceUpdate] = React.useState({});
React.useEffect(() => {
forceUpdate({});
}, []);

return (
<div style={{ background: 'rgba(0,0,0,0.1)', padding: 16 }}>
<h3>默认情况下不会自动删除添加的样式</h3>

<label>
<input type="checkbox" checked={show} onChange={() => setShow(!show)} />
Show Components
</label>

{show && (
<div>
<DesignTokenContext.Provider
value={{ cssVar: { key: 'default' }, hashed: true }}
>
<Button>Default</Button>
<Button type="primary">Primary</Button>
<Button type="ghost">Ghost</Button>

<Button className="btn-override">Override By ClassName</Button>
</DesignTokenContext.Provider>
<br />
<DesignTokenContext.Provider
value={{
token: { primaryColor: 'green' },
cssVar: { key: 'default2' },
hashed: true,
}}
>
<Button>Default</Button>
<Button type="primary">Primary</Button>
<Button type="ghost">Ghost</Button>

<Button className="btn-override">Override By ClassName</Button>
</DesignTokenContext.Provider>
</div>
)}
</div>
);
}
3 changes: 0 additions & 3 deletions docs/examples/layer.less

This file was deleted.

Loading

0 comments on commit 93ee282

Please sign in to comment.