Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add css and keyframes function #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<title>Solid With Emotion</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script src="./index.tsx" type="module"></script>
</body>
</html>
47 changes: 47 additions & 0 deletions demo/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { render } from 'solid-js/web'
import { styled, css, keyframes } from '../src'

const progress = keyframes`
0% {
width: 0;
}
50% {
width: 100%;
}
100% {
width: 0;
}
`

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

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

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

function App() {
return (
<div>
<h1>SolidJS With Emotion</h1>
<StyledSection as="section">
<p>
This is a section created with <code>styled</code> function.
</p>
<Animation />
</StyledSection>
</div>
)
}

render(App, document.getElementById('root')!)
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dist/**"
],
"scripts": {
"start": "vite",
"build": "rollup -c"
},
"devDependencies": {
Expand All @@ -29,7 +30,9 @@
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"solid-js": "^1.0.0",
"typescript": "^4.3.4"
"typescript": "^4.3.4",
"vite": "^2.6.14",
"vite-plugin-solid": "^2.1.2"
},
"peerDependencies": {
"solid-js": "^1.0.0"
Expand All @@ -40,4 +43,4 @@
"@emotion/serialize": "^1.0.2",
"@emotion/utils": "^1.0.0"
}
}
}
9 changes: 3 additions & 6 deletions src/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
getDefaultShouldForwardProp,
composeShouldForwardProps,
StyledOptions,
PrivateStyledComponent,
StyledElementType,
} from './utils'

Expand Down Expand Up @@ -223,14 +222,12 @@ const createStyled = (tag: any, options?: StyledOptions) => {
nextOptions,
true
),
})(
// @ts-ignore
...styles
)
})(...styles)
}

return Styled as Component<Props & { as?: string; class?: string }>
}
}

export default createStyled
// Don't use default export: https://github.com/solidjs/solid/issues/744
export { createStyled }
17 changes: 17 additions & 0 deletions src/css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Reference https://github.dev/emotion-js/emotion/blob/26ded6109fcd8ca9875cc2ce4564fee678a3f3c5/packages/react/src/css.js#L11
import type { SerializedStyles } from '@emotion/utils'
import { CSSInterpolation, serializeStyles } from '@emotion/serialize'

function css(
template: TemplateStringsArray,
...args: Array<CSSInterpolation>
): SerializedStyles
function css(...args: Array<CSSInterpolation>): SerializedStyles
function css(
...args: Array<TemplateStringsArray | CSSInterpolation>
): SerializedStyles {
// @ts-expect-error https://github.com/emotion-js/emotion/pull/2572
return serializeStyles(args)
}

export { css }
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import styledImport from './base'
import { createStyled } from './base'
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 = styledImport.bind()
const styled = createStyled.bind() as Styled &
Record<typeof tags[number], BoundStyled>

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

export { styled, css, keyframes }
24 changes: 24 additions & 0 deletions src/keyframes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Reference https://github.dev/emotion-js/emotion/blob/26ded6109fcd8ca9875cc2ce4564fee678a3f3c5/packages/react/src/keyframes.js#L24
import { css } from './css'
import { CSSInterpolation, Keyframes } from '@emotion/serialize'

function keyframes(
template: TemplateStringsArray,
...args: Array<CSSInterpolation>
): Keyframes
function keyframes(...args: Array<CSSInterpolation>): Keyframes
function keyframes(...args: any[]): Keyframes {
let insertable = css(...args)
const name = `animation-${insertable.name}`
// @ts-expect-error Keyframes type could never be fit as it unions string and object
return {
name,
styles: `@keyframes ${name}{${insertable.styles}}`,
anim: 1,
toString() {
return `_SOLID_EMOTION_${this.name}_${this.styles}_SOLID_EMOTION_`
},
}
}

export { 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
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"jsx": "preserve",
"jsxImportSource": "solid-js"
},
"include": ["src"]
"include": ["src", "demo"]
}
11 changes: 11 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'vite'
import solidPlugin from 'vite-plugin-solid'

export default defineConfig({
root: './demo',
plugins: [solidPlugin()],
build: {
target: 'esnext',
polyfillDynamicImport: false,
},
})