Skip to content

Commit

Permalink
feat: typed tag helper function on styled function
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed Dec 6, 2021
1 parent f4e80db commit 0fb96c6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ const progress = keyframes`
100% {
width: 0;
}
`;
`

const section = css`
background-color: #f6f8fa;
color: #333;
`

const StyledSection = styled('div')`
const StyledSection = styled.div`
${section}
margin: 20px 0;
padding: 20px;
`

const Animation = styled('div')`
const Animation = styled.div`
height: 20px;
background-color: #007cd3;
animation: ${progress} 2s linear infinite;
`;
`

function App() {
return (
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { tags } from './tags'
import { css } from './css'
import { keyframes } from './keyframes'

type Styled = OmitThisParameter<typeof createStyled>

type BoundStyled = ReturnType<Styled>

// bind it to avoid mutating the original function
// @ts-ignore
export const styled = createStyled.bind()
const styled = createStyled.bind() as Styled &
Record<typeof tags[number], BoundStyled>

tags.forEach((tagName: any) => {
;(styled as any)[tagName] = styled(tagName)
})

export { css, keyframes }
export { styled, css, keyframes }
2 changes: 1 addition & 1 deletion src/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ export const tags = [
'svg',
'text',
'tspan',
]
] as const
3 changes: 1 addition & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import solidPlugin from 'vite-plugin-solid'

export default defineConfig({
root: './demo',
// TOOD: Removing `hot` will crash dev environment.
plugins: [solidPlugin({hot: false})],
plugins: [solidPlugin()],
build: {
target: 'esnext',
polyfillDynamicImport: false,
Expand Down

0 comments on commit 0fb96c6

Please sign in to comment.