Skip to content

Commit

Permalink
refactor: override order (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Aug 19, 2022
1 parent 165ca70 commit 57b90f0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/examples/basic.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.btn-override {
background: rgb(255, 200, 200);
}
3 changes: 3 additions & 0 deletions docs/examples/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import Button from './components/Button';
import './basic.less';

export default function App() {
const [show, setShow] = React.useState(true);
Expand All @@ -23,6 +24,8 @@ export default function App() {
<Button>Default</Button>
<Button type="primary">Primary</Button>
<Button type="ghost">Ghost</Button>

<Button className="btn-override">Override By ClassName</Button>
</>
)}
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/StyleContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function createCache() {
const styles = document.body.querySelectorAll(`style[${ATTR_MARK}]`);

Array.from(styles).forEach((style) => {
(style as any)[CSS_IN_JS_INSTANCE] ??= CSS_IN_JS_INSTANCE_ID;
(style as any)[CSS_IN_JS_INSTANCE] =
(style as any)[CSS_IN_JS_INSTANCE] || CSS_IN_JS_INSTANCE_ID;
document.head.appendChild(style);
});

Expand Down
5 changes: 4 additions & 1 deletion src/useStyleRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ export default function useStyleRegister(
animationStatistics = {};

if (isMergedClientSide) {
const style = updateCSS(styleStr, styleId, { mark: ATTR_MARK });
const style = updateCSS(styleStr, styleId, {
mark: ATTR_MARK,
prepend: true,
});

(style as any)[CSS_IN_JS_INSTANCE] = CSS_IN_JS_INSTANCE_ID;

Expand Down
4 changes: 2 additions & 2 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ describe('csssinjs', () => {
const styles = Array.from(document.head.querySelectorAll('style'));
expect(styles).toHaveLength(2);

expect(styles[0].innerHTML).toBe('a{color:red;}div{color:blue;}');
expect(styles[1].innerHTML).toBe(
expect(styles[1].innerHTML).toBe('a{color:red;}div{color:blue;}');
expect(styles[0].innerHTML).toBe(
`.${hash} a{color:red;}.${hash} div{color:blue;}`,
);
});
Expand Down

0 comments on commit 57b90f0

Please sign in to comment.