Skip to content

Commit c65ad84

Browse files
committed
Implement styled
1 parent f3c7586 commit c65ad84

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

libs/extractor/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7756,6 +7756,23 @@ keyframes({
77567756
)
77577757
.unwrap()
77587758
));
7759+
7760+
reset_class_map();
7761+
assert_debug_snapshot!(ToBTreeSet::from(
7762+
extract(
7763+
"test.tsx",
7764+
r#"import {styled} from '@devup-ui/core'
7765+
const StyledDiv = styled.aside<{ test: string }>({ bg: "red" })
7766+
"#,
7767+
ExtractOption {
7768+
package: "@devup-ui/core".to_string(),
7769+
css_dir: "@devup-ui/core".to_string(),
7770+
single_css: false,
7771+
import_main_css: false
7772+
}
7773+
)
7774+
.unwrap()
7775+
));
77597776
}
77607777

77617778
// #[test]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {styled} from '@devup-ui/core'\n const StyledDiv = styled.aside<{ test: string }>({ bg: \"red\" })\n \"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: false, import_main_css: false\n}).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "background",
10+
value: "red",
11+
level: 0,
12+
selector: None,
13+
style_order: None,
14+
},
15+
),
16+
},
17+
code: "import \"@devup-ui/core/devup-ui-0.css\";\nconst StyledDiv = ({ style, className, ...rest }) => <aside {...rest} className={[\"a-a\", className].filter(Boolean).join(\" \")} style={style} />;\n",
18+
}

packages/react/src/__tests__/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('export', () => {
1515
css: expect.any(Function),
1616
globalCss: expect.any(Function),
1717
keyframes: expect.any(Function),
18+
styled: expect.any(Function),
1819

1920
ThemeScript: expect.any(Function),
2021

packages/react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ export { globalCss } from './utils/global-css'
2121
export { initTheme } from './utils/init-theme'
2222
export { keyframes } from './utils/keyframes'
2323
export { setTheme } from './utils/set-theme'
24+
export { styled } from './utils/styled'
2425
export type { CustomColors } from 'csstype-extra'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { styled } from '../styled'
2+
3+
describe('globalCss', () => {
4+
it('should return className', () => {
5+
expect(() => styled.div`virtual-css`).toThrowError(
6+
'Cannot run on the runtime',
7+
)
8+
})
9+
})

packages/react/src/utils/styled.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { DevupPropsWithTheme } from '../types/props'
2+
3+
interface StyledCreator {
4+
<T extends React.ElementType | React.ComponentType>(
5+
tag: T,
6+
): (
7+
strings: TemplateStringsArray | DevupPropsWithTheme,
8+
) => (props: React.ComponentProps<T>) => React.ReactElement
9+
}
10+
11+
type Styled = StyledCreator & {
12+
[T in keyof React.JSX.IntrinsicElements]: <P extends React.ComponentProps<T>>(
13+
strings: TemplateStringsArray | DevupPropsWithTheme,
14+
) => (props: P) => React.ReactElement
15+
}
16+
17+
export const styled: Styled = new Proxy(Function.prototype, {
18+
get() {
19+
return () => {
20+
throw new Error('Cannot run on the runtime')
21+
}
22+
},
23+
}) as any

0 commit comments

Comments
 (0)