Skip to content

Commit 06bb2d0

Browse files
authored
Merge pull request #752 from sungik-choi/fix/styled-func
styled-components로 생성되는 className에 namespace 추가, 잘못된 FoundationStyled의 구현 수정
2 parents 842bb48 + bfb5c51 commit 06bb2d0

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

package-lock.json

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"tslib": "^2.3.1",
143143
"ttypescript": "^1.5.13",
144144
"typescript": "^4.4.3",
145+
"typescript-plugin-styled-components": "^2.0.0",
145146
"typescript-transform-paths": "^3.3.1"
146147
},
147148
"peerDependencies": {

rollup.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@ import commonjs from '@rollup/plugin-commonjs'
66
import babel from '@rollup/plugin-babel'
77
import { visualizer } from 'rollup-plugin-visualizer'
88
import typescript from 'rollup-plugin-typescript2'
9+
import createStyledComponentsTransformer from 'typescript-plugin-styled-components'
910

1011
import packageJson from './package.json'
1112

1213
const extensions = DEFAULT_EXTENSIONS.concat(['.ts', '.tsx'])
1314

15+
const styledComponentsTransformer = createStyledComponentsTransformer({
16+
displayName: true,
17+
})
18+
1419
// Order Matters, must after rollup-plugin-node-resolve
1520
// See Also: https://www.npmjs.com/package/rollup-plugin-typescript2
1621
const typescriptPlugin = typescript({
1722
typescript: require('ttypescript'),
1823
tsconfig: './tsconfig.json',
1924
useTsconfigDeclarationDir: true,
2025
declarationDir: './build/src',
26+
transformers: [
27+
() => ({
28+
before: [styledComponentsTransformer],
29+
}),
30+
],
2131
tsconfigDefaults: {
2232
noEmit: false,
2333
emitDeclarationOnly: true,

src/foundation/FoundationStyledComponent.tsx

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,38 +76,33 @@ interface FoundationStyledInterface extends FoundationStyledComponentFactories {
7676
): ThemedStyledFunction<C, Foundation, { foundation?: Foundation }>
7777
}
7878

79+
function templateFunctionGenerator(BaseComponentGenerator: ThemedStyledFunction<any, any, object, string | number | symbol>) {
80+
const customTemplateFn = (...args: TemplateStringsArray) => {
81+
// @ts-ignore
82+
const BaseComponent = BaseComponentGenerator(...args)
83+
const BaseRefComponent = forwardRef((props, ref) => {
84+
const currentFoundation = useContext(FoundationContext)
85+
return (
86+
<BaseComponent
87+
ref={ref}
88+
key={args.toString()}
89+
foundation={currentFoundation}
90+
{...props}
91+
/>
92+
)
93+
})
94+
BaseRefComponent.toString = BaseComponent.toString
95+
return BaseRefComponent
96+
}
97+
customTemplateFn.attrs = (attrs) => templateFunctionGenerator(BaseComponentGenerator.attrs(attrs))
98+
customTemplateFn.withConfig = (config) => templateFunctionGenerator(BaseComponentGenerator.withConfig(config))
99+
return customTemplateFn
100+
}
101+
79102
/* eslint-disable-next-line func-names */ /* @ts-ignore */
80103
const FoundationStyled: FoundationStyledInterface = (tag) => {
81104
const tagTemplate = styled(tag)
82-
83-
function templateFunctionGenerator(BaseComponentGenerator) {
84-
return function customTemplateFunction(...args: TemplateStringsArray) {
85-
const BaseComponent = BaseComponentGenerator(...args)
86-
87-
const BaseRefComponent = forwardRef((props, ref) => {
88-
const currentFoundation = useContext(FoundationContext)
89-
return (
90-
<BaseComponent
91-
ref={ref}
92-
key={args.toString()}
93-
foundation={currentFoundation}
94-
{...props}
95-
/>
96-
)
97-
})
98-
BaseRefComponent.toString = BaseComponent.toString
99-
100-
return BaseRefComponent
101-
}
102-
}
103-
104-
const wrappedTemplateFunction = templateFunctionGenerator(tagTemplate)
105-
// @ts-ignore
106-
wrappedTemplateFunction.attrs = attrs => templateFunctionGenerator(tagTemplate.attrs(attrs))
107-
// @ts-ignore
108-
wrappedTemplateFunction.withConfig = config => templateFunctionGenerator(tagTemplate.withConfig(config))
109-
110-
return wrappedTemplateFunction
105+
return templateFunctionGenerator(tagTemplate)
111106
};
112107

113108
(domElements as Array<AnyStyledComponent>).forEach(element => {

0 commit comments

Comments
 (0)