Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1d6cde5
Removed Flow (and its tooling) from the source code (#2385)
May 23, 2021
73073f3
Merge branch 'main' into ts-migration
Andarist May 24, 2021
4912b82
Add TypeScript tooling and configs (#2360)
with-heart Jun 25, 2021
7f8db2d
Migrate `@emotion/memoize` to TypeScript (#2427)
sarayourfriend Aug 14, 2021
b1d16b0
Migrate `@emotion/weak-memoize` to TypeScript (#2429)
sarayourfriend Aug 14, 2021
3119b05
Fix `types` field in recently converted packages
Andarist Aug 14, 2021
fc4d27c
Migrate `@emotion/unitless` to TypeScript (#2430)
sarayourfriend Aug 14, 2021
a1e881b
Migrate `@emotion/is-prop-valid` to TypeScript (#2432)
sarayourfriend Aug 14, 2021
52aadc6
Migrate `@emotion/sheet` to TypeScript (#2431)
sarayourfriend Aug 14, 2021
16d8a8c
Migrate `@emotion/utils` to TypeScript (#2359)
rjdestigter Aug 15, 2021
ea2c397
Migrate `@emotion/hash` to TypeScript (#2454)
Andarist Aug 15, 2021
e3a4919
Use `import type` when importing `StyleSheet` from `@emotion/sheet` i…
Andarist Aug 15, 2021
d48d7f6
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura Nov 14, 2021
ce3bccb
Fix TS error and test failure
srmagura Nov 14, 2021
30f982c
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura Nov 14, 2021
e0cb8ab
Remove some Flow type annotations
srmagura Nov 14, 2021
fb0d461
Remove Flow type annotations from benchmark
srmagura Nov 14, 2021
908703b
Remove .orig files (accidentally committed)
srmagura Nov 14, 2021
7d56dbf
Merge pull request #2546 from srmagura/ts-migration
emmatown Nov 19, 2021
f3ef8cb
Delete .d.ts files that shouldn't exist
emmatown Nov 19, 2021
da3f244
Merge branch 'main' into ts-migration
emmatown Nov 20, 2021
85772c3
Migrate @emotion/css to TypeScript (#2558)
emmatown Nov 23, 2021
c4bff2e
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura Feb 19, 2022
0646268
Merge branch 'main' of https://github.com/srmagura/emotion into ts-mi…
srmagura Mar 16, 2022
ea84c40
Migrate `@emotion/babel-plugin-jsx-pragmatic` to TypeScript (#2570)
G-Rath Mar 18, 2022
5e46164
Merge branch 'ts-migration' of https://github.com/emotion-js/emotion …
srmagura Mar 18, 2022
e959f38
Merge pull request #2655 from srmagura/ts-migration-merge
Andarist Mar 19, 2022
f99981b
Migrate `babel-tester` to TypeScript (#2688)
Andarist Apr 9, 2022
304f7e3
Migrate `@emotion/eslint-plugin` to TypeScript (#2568)
G-Rath Apr 11, 2022
3045a2f
Rename a duplicate `typescript` job to `dtslint`
Andarist Apr 18, 2022
4c56bcb
Run the main CI job on the `ts-migration` branch too
Andarist Apr 18, 2022
17fee4b
Fix some TS things (#2559)
emmatown Apr 19, 2022
9ca22c6
Migrate `@emotion/serialize` to TypeScript (#2543)
danilofuchs Apr 19, 2022
a22d43b
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura May 21, 2022
8c4e2c3
Merge pull request #2758 from srmagura/ts-merge2
Andarist May 21, 2022
f01f9bc
Remove accidentally-committed .d.ts files (#2760)
srmagura May 21, 2022
8ed0042
Fix `d.ts` emit in `@emotion/eslint-plugin` (#2761)
srmagura May 22, 2022
8c6ad60
Merge branch 'main' into ts-merge-0710
srmagura Jul 10, 2022
95726a9
Merge pull request #2816 from srmagura/ts-merge-0710
srmagura Jul 10, 2022
945169b
Fixed `dtslint` tasks for `@emotion/cache` and `@emotion/server` (#2817)
srmagura Jul 26, 2022
8546dd0
Convert `@emotion/primitives-core` to TypeScript (#2818)
srmagura Jul 31, 2022
62a513b
Add a changeset for `@emotion/primitives-core` and the missing `types…
Andarist Aug 2, 2022
5fedb73
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura Aug 6, 2022
5cd2199
Merge pull request #2846 from srmagura/ts-merge-0806
srmagura Aug 6, 2022
62e49ad
Move `site` outside of the main typecheck task (#2851)
srmagura Aug 16, 2022
993557d
Remove accidentally comitted `src/*.d.ts` files in already converted …
Andarist Aug 16, 2022
1effaf9
Merge remote-tracking branch 'origin/main' into ts-migration
Andarist May 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
…migration-merge

Conflicts:
	.github/workflows/main.yml
	jest.config.js
	package.json
	packages/css/test/instance/stream.test.js
	packages/primitives/test/emotion-primitives.test.js
	packages/react/__tests__/global-with-theme.js
	packages/react/__tests__/global.js
	packages/react/__tests__/rehydration.js
	packages/react/src/class-names.js
	packages/react/src/get-label-from-stack-trace.js
	packages/react/src/global.js
	packages/server/test/index.test.js
	packages/server/test/inline.test.js
	packages/server/test/stream.test.js
	packages/server/test/util.js
	packages/utils/types/index.d.ts
	test/styleTransform.js
	yarn.lock
  • Loading branch information
srmagura committed Feb 19, 2022
commit c4bff2e5ed071ba33cbe92610b6339110755682f
5 changes: 0 additions & 5 deletions .changeset/sweet-hotels-explain.md

This file was deleted.

64 changes: 63 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,74 @@ jobs:
run: yarn

- name: Run Tests
run: yarn coverage --color
run: yarn test:ci --color

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
if: ${{ matrix.platform == 'ubuntu-latest' && always() }}

typescript:
name: TypeScript
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main

- name: Set Node.js 12.x
uses: actions/setup-node@main
with:
node-version: 12.x

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache
with:
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Install Dependencies
run: yarn

- name: Check Types
run: yarn tsc

test_react18:
name: Test React 18
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main

- name: Set Node.js 12.x
uses: actions/setup-node@main
with:
node-version: 12.x

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache
with:
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Install Dependencies
run: yarn

- name: Run Tests with React 18
run: yarn test:react18:ci

test_dist:
name: Test Dist
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public/
!playgrounds/cra/public
.env
.vscode
.parcel-cache/
.parcel-cache/
*.orig
2 changes: 1 addition & 1 deletion docs/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: 'Install'
---

There are lots of ways to use Emotion, if you're using React, the easiest way to get started is to use the [`@emotion/react` package](/packages/react). If you're not using React, you should use [the `emotion` package](#vanilla).
There are lots of ways to use Emotion, if you're using React, the easiest way to get started is to use the [`@emotion/react` package](/packages/react). If you're not using React, you should use [the `@emotion/css` package](#vanilla).

```bash
yarn add @emotion/react
Expand Down
2 changes: 1 addition & 1 deletion docs/styled.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const H1 = styled('h1', {
shouldForwardProp: prop =>
isPropValid(prop) && prop !== 'color'
})(props => ({
color: 'hotpink'
color: props.color
}))

render(<H1 color="lightgreen">This is lightgreen.</H1>)
Expand Down
92 changes: 35 additions & 57 deletions docs/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
title: 'TypeScript'
---

Emotion includes TypeScript definitions for `@emotion/react` and `@emotion/styled`. These definitions also infer types for css properties with the object syntax, HTML/SVG tag names, and prop types.
Emotion includes TypeScript definitions for `@emotion/react` and `@emotion/styled`. These definitions infer types for css properties with the object syntax, HTML/SVG tag names, and prop types.

## @emotion/react

The easiest way to use the css prop with TypeScript is with the new JSX transform and the `jsxImportSource` TSConfig option (available since TS 4.1). For this approach, your TSConfig `compilerOptions` should contain

```json
"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"
```

For most users, this is all the setup that is required. You can now define styles using the object syntax or template literal syntax and pass them to your components via the `css` prop.

```tsx
import { css } from '@emotion/react'

Expand All @@ -22,7 +31,7 @@ const subtitleStyle = css`
`
```

TypeScript checks css properties with the object style syntax using [csstype](https://www.npmjs.com/package/csstype) package, so following code will emit errors.
Object styles are recommended since they are type checked with the help of the [csstype](https://www.npmjs.com/package/csstype) package. For example, the following code will emit an error.

```tsx
import { css } from '@emotion/react';
Expand All @@ -35,47 +44,41 @@ const titleStyle = css({
});
```

To make the css prop work with pure TypeScript (without babel plugin) you need to add `/** @jsx jsx */` at the top of every file that is using the css prop:
When using our JSX factory, TypeScript only allows the `css` prop on components that accept a `className` prop. This is because `@emotion/react` resolves the value of the `css` prop to a class name and then passes this class name down to the rendered component.

### With the Babel plugin

[`@emotion/babel-plugin`](/docs/babel) is completely optional for TypeScript users. If you are not already using Babel, you probably shouldn't add it to your build tooling unless you truly need one of the features offered by `@emotion/babel-plugin`. On the other hand, there's no reason not to use `@emotion/babel-plugin` if you are already using Babel to transpile your TypeScript code.

### With the old JSX transform

If you are unable to upgrade to the `react-jsx` transform, you will need to specify the JSX factory at the top of every file:

```tsx
/** @jsx jsx */
import { jsx } from '@emotion/react'

<div css={{ background: 'black' }} />
```

As a result you may be not able to use react fragment shorthand syntax - `<></>`, but still you can use `<Fragment></Fragment>`.
This is a limitation of the TypeScript compiler not being able to independently specify jsx pragma and jsxFrag pragma.
As a result, you may be not able to use the shorthand syntax `<></>` for React fragments, but you can still use `<Fragment></Fragment>`. This is a limitation of the TypeScript compiler not being able to independently specify jsx pragma and jsxFrag pragma.

You can still use the css helper and pass the className yourself (ensure you are importing from the `@emotion/css` package, not `@emotion/react`).

```tsx
import { css } from '@emotion/css'

<div className={css({ background: 'black' })} />
const el = <div className={css({ background: 'black' })} />
```

### `css` prop

When using our JSX factories the support for `css` prop is being added only for components that accepts `className` prop as they take provided `css` prop, resolves it and pass the generated `className` to the rendered component.

If using the automatic runtime you should just add this to your `tsconfig.json` to let TypeScript know where it should look for the `JSX` namespace:
```json
{
"compilerOptions": {
"jsxImportSource": "@emotion/react"
}
}
```

The same `JSX` namespace is resolved if you are still using the classic runtime through the `@jsx` pragma. However, it's not possible to leverage `css` prop support being added conditionally based on a type of rendered component when one is not using our jsx pragma or the automatic runtime. For those cases when people use our pragma implicitly (for example when using our `@emotion/babel-preset-css-prop`) we have a special file that can be imported once to add support for the `css` prop globally, for all components. Use it like this:
It's not possible to leverage `css` prop support being added conditionally based on the type of a rendered component when not using our jsx pragma or the `react-jsx` transform. If you use our pragma implicitly (for example when using our `@emotion/babel-preset-css-prop`) we have a special file that can be imported once to add support for the `css` prop globally, for all components. Use it like this:

```ts
/// <reference types="@emotion/react/types/css-prop" />
```

## @emotion/styled

`@emotion/styled` works with TypeScript without any additional configuration.

### HTML/SVG elements

```tsx
Expand Down Expand Up @@ -170,13 +173,12 @@ interface ComponentProps {
label: string
}

const Component: FC<ComponentProps> = ({
label,
className
}) => <div className={className}>{label}</div>
const Component: FC<ComponentProps> = ({ label, className }) => (
<div className={className}>{label}</div>
)

const StyledComponent0 = styled(Component)`
color: ${props => props.label === 'Important' ? 'red' : 'green'};
color: ${props => (props.label === 'Important' ? 'red' : 'green')};
`

const StyledComponent1 = styled(Component)({
Expand All @@ -195,12 +197,12 @@ const App = () => (

Sometimes you want to wrap an existing component and override the type of a prop. Emotion allows you to specify a `shouldForwardProp` hook to filter properties which should be passed to the wrapped component.

If you make `shouldForwardProp` a [type guard](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) then only the props from the type guard will be exposed.
If you make `shouldForwardProp` a [type guard](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates) then only the props from the type guard will be exposed.

For example:

``` ts
const Original: React.FC<{ prop1: string, prop2: string }> = () => null
```ts
const Original: React.FC<{ prop1: string; prop2: string }> = () => null

interface StyledOriginalExtraProps {
// This prop would conflict with the `prop2` on Original
Expand Down Expand Up @@ -286,7 +288,7 @@ declare module '@emotion/react' {

// You are also able to use a 3rd party theme this way:
import '@emotion/react'
import { LibTheme } from 'some-lib'
import { LibTheme } from 'some-lib'

declare module '@emotion/react' {
export interface Theme extends LibTheme {}
Expand All @@ -307,38 +309,14 @@ const Button = styled('button')`
export default Button
```

If you were previously relying on `theme` being an `any` type, you have to restore compatibility with:
If you were previously relying on `theme` being an `any` type, you can restore compatibility with:

_emotion.d.ts_

```ts
import '@emotion/react'

declare module '@emotion/react' {
export interface Theme extends Record<string, any> {}
export interface Theme extends Record<string, any> {}
}
```

### TypeScript < 2.9

For Typescript <2.9, the generic type version only works with object styles due to https://github.com/Microsoft/TypeScript/issues/11947.

You can work around this by specifying the prop types in your style callback:

``` ts
const StyledComponent0 = styled(Component)`
color: red;
background: ${(props: StyledComponentProps) =>
props.bgColor};
`
```

NOTE: This approach you will have to perform the intersection with the component props yourself to get at the component props

``` ts
const StyledComponent0 = styled(Component)`
color: red;
background: ${(props: StyledComponentProps & ComponentProps) =>
props.bgColor};
`
```
9 changes: 9 additions & 0 deletions jest-react18.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const baseConfig = require('./jest.config.js')

module.exports = Object.assign({}, baseConfig, {
moduleNameMapper: {
'^react($|\\/.+)': 'react18$1',
'^react-dom($|\\/.+)': 'react18-dom$1',
'^react-test-renderer($|\\/.+)': 'react18-test-renderer$1'
}
})
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
testEnvironment: 'jsdom',
transform: {
'\\.css$': '<rootDir>/test/styleTransform.js',
'^.+\\.(tsx|ts|js)?$': 'babel-jest'
},
watchPlugins: [
Expand Down
2 changes: 0 additions & 2 deletions jest.dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ const baseConfig = require('./jest.config.js')
module.exports = Object.assign({}, baseConfig, {
transformIgnorePatterns: ['dist', 'node_modules']
})

delete module.exports.moduleNameMapper
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"test:size": "npm-run-all build size",
"test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand --watch",
"test": "jest",
"test:react18": "jest -c jest-react18.config.js",
"test:typescript": "yarn workspaces run test:typescript",
"coverage": "jest --coverage --no-cache --ci --runInBand",
"test:ci": "jest --coverage --no-cache --ci --runInBand",
"test:react18:ci": "yarn test:react18 --coverage --no-cache --ci --runInBand",
"test:prod": "yarn build && jest -c jest.dist.js --no-cache --ci --runInBand",
"lint:check": "eslint .",
"test:watch": "jest --watch",
Expand Down Expand Up @@ -195,15 +197,15 @@
"@mdx-js/mdx": "^1.6.22",
"@mdx-js/react": "^1.6.22",
"@preconstruct/cli": "1.1.34",
"@testing-library/react": "^11.0.4",
"@types/jest": "^26.0.14",
"@testing-library/react": "13.0.0-alpha.5",
"@types/jest": "^27.0.3",
"@types/node": "^10.11.4",
"@types/react": "^16.9.11",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"babel-check-duplicated-nodes": "^1.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.3.0",
"babel-jest": "^27.4.5",
"babel-plugin-add-basic-constructor-for-react-components": "^0.1.0",
"babel-plugin-codegen": "^3.0.0",
"babel-plugin-fix-dce-for-classes-with-statics": "^0.1.0",
Expand All @@ -228,13 +230,12 @@
"eslint-plugin-react-hooks": "^4.1.2",
"html-tag-names": "^1.1.2",
"husky": "^3.0.9",
"jest": "^26.4.2",
"jest-environment-jsdom": "^26.3.0",
"jest": "^27.4.5",
"jest-environment-jsdom": "^27.4.4",
"jest-in-case": "^1.0.2",
"jest-junit": "^11.1.0",
"jest-serializer-html": "^7.0.0",
"jest-watch-typeahead": "^0.6.1",
"jsdom": "^16.4.0",
"jest-junit": "^13.0.0",
"jest-serializer-html": "^7.1.0",
"jest-watch-typeahead": "^1.0.0",
"lint-staged": "^7.2.0",
"module-alias": "^2.0.1",
"multipipe": "^1.0.2",
Expand All @@ -254,8 +255,9 @@
"react-primitives": "^0.8.1",
"react-router-dom": "^4.2.2",
"react-test-renderer": "16.8.6",
"react18": "npm:react@alpha",
"react18-dom": "npm:react-dom@alpha",
"react18": "npm:[email protected]",
"react18-dom": "npm:[email protected]",
"react18-test-renderer": "npm:[email protected]",
"svg-tag-names": "^1.1.1",
"through": "^2.3.8",
"typescript": "^4.2.4",
Expand Down
16 changes: 16 additions & 0 deletions packages/babel-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# @emotion/babel-plugin

## 11.7.2

### Patch Changes

- [#2585](https://github.com/emotion-js/emotion/pull/2585) [`b830c7dc`](https://github.com/emotion-js/emotion/commit/b830c7dc9da1c75c88e655150f04ef52b8176212) Thanks [@Andarist](https://github.com/Andarist)! - Fixed label extraction crashing in some cases involving variable declarations with array and object patterns.

* [#2585](https://github.com/emotion-js/emotion/pull/2585) [`b830c7dc`](https://github.com/emotion-js/emotion/commit/b830c7dc9da1c75c88e655150f04ef52b8176212) Thanks [@kddc](https://github.com/kddc), [@Andarist](https://github.com/Andarist)! - Improved label extraction for named function expressions and anonymous functions used as object property values.

- [#2602](https://github.com/emotion-js/emotion/pull/2602) [`b02f349d`](https://github.com/emotion-js/emotion/commit/b02f349d28df7bc77cad6d7e1b62aecef9f19405) Thanks [@Andarist](https://github.com/Andarist)! - Fixed an issue with styled transformer sometimes not using the used local name for the imported named export when used with `importMap`.

## 11.7.1

### Patch Changes

- [#2590](https://github.com/emotion-js/emotion/pull/2590) [`1554a7e2`](https://github.com/emotion-js/emotion/commit/1554a7e264e05780b2c5bd74ccb20a92005ba61d) Thanks [@Andarist](https://github.com/Andarist)! - Upgraded and pinned the version of Stylis - the CSS parser that Emotion uses under the hood.

## 11.3.0

### Minor Changes
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.