forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(postcss): add support for mixins to postcss-preset-primer (prime…
…r#4856) * feat(postcss): add support for mixins to postcss-preset-primer * chore(deps): add @types/postcss-mixins * fix: update order of plugins so that mixins can be transformed --------- Co-authored-by: Josh Black <[email protected]>
- Loading branch information
Showing
10 changed files
with
680 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
packages/postcss-preset-primer/config/transform/jsTransform.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import babelJest from 'babel-jest' | ||
|
||
const babelOptions = { | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
targets: { | ||
node: 'current', | ||
}, | ||
}, | ||
], | ||
'@babel/preset-typescript', | ||
], | ||
plugins: ['babel-plugin-transform-import-meta'], | ||
} | ||
|
||
export default babelJest.createTransformer(babelOptions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-disable github/unescaped-html-literal */ | ||
|
||
'use strict' | ||
|
||
/** | ||
* @type {import('jest').Config} | ||
*/ | ||
const config = { | ||
setupFiles: [], | ||
setupFilesAfterEnv: [], | ||
testMatch: ['<rootDir>/**/*.test.[jt]s?(x)', '!**/*.types.test.[jt]s?(x)'], | ||
transform: { | ||
'^.+\\.(js|ts|tsx)$': require.resolve('./config/transform/jsTransform.js'), | ||
}, | ||
transformIgnorePatterns: [], | ||
} | ||
|
||
module.exports = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/postcss-preset-primer/src/__tests__/__snapshots__/preset.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`postcss-preset-primer css nesting 1`] = ` | ||
" | ||
.selector[data-active] { | ||
color: var(--fgColor-active); | ||
} | ||
" | ||
`; | ||
|
||
exports[`postcss-preset-primer mixins @mixin focusOutline 1`] = ` | ||
" | ||
.selector { | ||
outline: 2px solid var(--focus-outlineColor); | ||
outline-offset: -2px; | ||
box-shadow: none; | ||
} | ||
" | ||
`; | ||
|
||
exports[`postcss-preset-primer mixins @mixin focusOutlineOnEmphasis 1`] = ` | ||
" | ||
.selector { | ||
outline: 2px solid var(--focus-outlineColor); | ||
outline-offset: -2px; | ||
box-shadow: inset 0 0 0 3px var(--fgColor-onEmphasis); | ||
} | ||
" | ||
`; |
61 changes: 61 additions & 0 deletions
61
packages/postcss-preset-primer/src/__tests__/preset.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import postcss from 'postcss' | ||
import preset from '../' | ||
|
||
async function process(input: string): Promise<string> { | ||
try { | ||
const result = await postcss([preset()]).process(input, { | ||
from: undefined, | ||
}) | ||
return result.css.toString() | ||
} catch (error) { | ||
// Note: it seems like Jest cannot serialize errors thrown by postcss. As a | ||
// result, we remove problematic fields before throwing the error | ||
if (error instanceof postcss.CssSyntaxError) { | ||
// @ts-expect-error it seems the type for this is incorrect and this field | ||
// does exist and is causing the circular reference issue | ||
if (error.postcssNode) { | ||
// @ts-expect-error it seems the type for this is incorrect and this field | ||
// does exist and is causing the circular reference issue | ||
delete error.postcssNode | ||
} | ||
} | ||
throw error | ||
} | ||
} | ||
|
||
describe('postcss-preset-primer', () => { | ||
test('css nesting', async () => { | ||
const result = await process(` | ||
.selector { | ||
&[data-active] { | ||
color: var(--fgColor-active); | ||
} | ||
} | ||
`) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
describe('mixins', () => { | ||
test('@mixin focusOutline', async () => { | ||
const result = await process(` | ||
.selector { | ||
@mixin focusOutline; | ||
} | ||
`) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
test('@mixin focusOutlineOnEmphasis', async () => { | ||
const result = await process(` | ||
.selector { | ||
@mixin focusOutlineOnEmphasis; | ||
} | ||
`) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@define-mixin focusOutline $outlineOffset: -2px, $outlineColor: var(--focus-outlineColor) { | ||
outline: 2px solid $outlineColor; | ||
outline-offset: $outlineOffset; | ||
box-shadow: none; | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/postcss-preset-primer/src/mixins/focusOutlineOnEmphasis.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* outline with fg box-shadow for buttons */ | ||
@define-mixin focusOutlineOnEmphasis $outlineOffset: -2px, $outlineColor: var(--focus-outlineColor) { | ||
outline: 2px solid $outlineColor; | ||
outline-offset: $outlineOffset; | ||
box-shadow: inset 0 0 0 3px var(--fgColor-onEmphasis); | ||
} |