generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: useInsertionEffect if in React 18 (#121)
* fix: order of memo & effect * chore: mv effect style in * chore: add test case * test: add 17 test * chore: clean up
- Loading branch information
Showing
4 changed files
with
148 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// import canUseDom from 'rc-util/lib/Dom/canUseDom'; | ||
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect'; | ||
import * as React from 'react'; | ||
|
||
// We need fully clone React function here | ||
// to avoid webpack warning React 17 do not export `useId` | ||
const fullClone = { | ||
...React, | ||
}; | ||
const { useInsertionEffect } = fullClone; | ||
const useMergedInsertionEffect = useInsertionEffect || useLayoutEffect; | ||
|
||
export default useMergedInsertionEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { render } from '@testing-library/react'; | ||
import * as React from 'react'; | ||
import type { CSSInterpolation } from '../src'; | ||
import { Theme, useCacheToken, useStyleRegister } from '../src'; | ||
|
||
interface DesignToken { | ||
primaryColor: string; | ||
} | ||
|
||
interface DerivativeToken extends DesignToken { | ||
primaryColorDisabled: string; | ||
} | ||
|
||
const derivative = (designToken: DesignToken): DerivativeToken => ({ | ||
...designToken, | ||
primaryColorDisabled: designToken.primaryColor, | ||
}); | ||
|
||
const baseToken: DesignToken = { | ||
primaryColor: '#1890ff', | ||
}; | ||
|
||
const theme = new Theme(derivative); | ||
|
||
vi.mock('react', async () => { | ||
const origin: any = await vi.importActual('react'); | ||
|
||
return { | ||
...origin, | ||
useInsertionEffect: undefined, | ||
}; | ||
}); | ||
|
||
// Same as `index.spec.tsx` but we hack to no to support `useInsertionEffect` | ||
describe('legacy React version', () => { | ||
beforeEach(() => { | ||
const styles = Array.from(document.head.querySelectorAll('style')); | ||
styles.forEach((style) => { | ||
style.parentNode?.removeChild(style); | ||
}); | ||
}); | ||
|
||
const genStyle = (token: DerivativeToken): CSSInterpolation => ({ | ||
'.box': { | ||
width: 93, | ||
lineHeight: 1, | ||
backgroundColor: token.primaryColor, | ||
}, | ||
}); | ||
|
||
interface BoxProps { | ||
propToken?: DesignToken; | ||
} | ||
|
||
const Box = ({ propToken = baseToken }: BoxProps) => { | ||
const [token] = useCacheToken<DerivativeToken>(theme, [propToken]); | ||
|
||
useStyleRegister({ theme, token, path: ['.box'] }, () => [genStyle(token)]); | ||
|
||
return <div className="box" />; | ||
}; | ||
|
||
// We will not remove style immediately, | ||
// but remove when second style patched. | ||
describe('remove old style to ensure style set only exist one', () => { | ||
function test( | ||
name: string, | ||
wrapperFn?: (node: React.ReactElement) => React.ReactElement, | ||
beforeFn?: () => void, | ||
) { | ||
it(name, () => { | ||
beforeFn?.(); | ||
|
||
const getBox = (props?: BoxProps) => { | ||
const box: React.ReactElement = <Box {...props} />; | ||
|
||
return wrapperFn?.(box) || box; | ||
}; | ||
|
||
const { rerender } = render(getBox()); | ||
expect(document.head.querySelectorAll('style')).toHaveLength(1); | ||
|
||
// First change | ||
rerender( | ||
getBox({ | ||
propToken: { | ||
primaryColor: 'red', | ||
}, | ||
}), | ||
); | ||
expect(document.head.querySelectorAll('style')).toHaveLength(1); | ||
|
||
// Second change | ||
rerender( | ||
getBox({ | ||
propToken: { | ||
primaryColor: 'green', | ||
}, | ||
}), | ||
); | ||
expect(document.head.querySelectorAll('style')).toHaveLength(1); | ||
}); | ||
} | ||
|
||
test('normal'); | ||
|
||
test('StrictMode', (ele) => <React.StrictMode>{ele}</React.StrictMode>); | ||
}); | ||
}); |