Skip to content

Commit 270ddaa

Browse files
authored
Migrate postcss-focus-within (#32)
1 parent 8d74b85 commit 270ddaa

File tree

17 files changed

+98
-250
lines changed

17 files changed

+98
-250
lines changed

plugins/postcss-focus-within/.editorconfig

Lines changed: 0 additions & 15 deletions
This file was deleted.

plugins/postcss-focus-within/.gitattributes

Lines changed: 0 additions & 1 deletion
This file was deleted.

plugins/postcss-focus-within/.github/workflows/test.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
dist
21
node_modules
2+
dist
33
package-lock.json
44
yarn.lock
55
*.log*
66
*.result.css
7-
.*
7+
*.result.css.map
88
!.editorconfig
9-
!.gitattributes
109
!.gitignore
1110
!.rollup.js
1211
!.tape.js
13-
!.github
12+
!.travis.yml

plugins/postcss-focus-within/.rollup.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

plugins/postcss-focus-within/CONTRIBUTING.md

Lines changed: 0 additions & 65 deletions
This file was deleted.

plugins/postcss-focus-within/INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,6 @@ grunt.initConfig({
165165
[PostCSS]: https://github.com/postcss/postcss
166166
[PostCSS CLI]: https://github.com/postcss/postcss-cli
167167
[PostCSS Loader]: https://github.com/postcss/postcss-loader
168-
[PostCSS]: https://github.com/csstools/postcss-focus-within
168+
[PostCSS]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-focus-within
169169
[React App Rewire PostCSS]: https://github.com/csstools/react-app-rewire-postcss
170170
[React App Rewired]: https://github.com/timarney/react-app-rewired

plugins/postcss-focus-within/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[<img alt="npm version" src="https://img.shields.io/npm/v/postcss-focus-within.svg" height="20">][npm-url]
44
[<img alt="CSS Standard Status" src="https://cssdb.org/badge/focus-within-pseudo-class.svg" height="20">][css-url]
5-
[<img alt="Build Status" src="https://github.com/csstools/postcss-focus-within/workflows/test/badge.svg" height="20">][cli-url]
5+
[<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/tree/main/postcss-focus-within/workflows/test/badge.svg" height="20">][cli-url]
66
[<img alt="support chat" src="https://img.shields.io/badge/support-chat-blue.svg" height="20">][git-url]
77

88
[PostCSS Focus Within] lets you use the `:focus-within` pseudo-class in CSS,
@@ -116,14 +116,14 @@ focusWithin({ replaceWith: '.focus-within' });
116116
```
117117

118118
[css-url]: https://cssdb.org/#focus-within-pseudo-class
119-
[cli-url]: https://github.com/csstools/postcss-focus-within/actions/workflows/test.yml?query=workflow/test
119+
[cli-url]: https://github.com/csstools/postcss-plugins/tree/main/postcss-focus-within/actions/workflows/test.yml?query=workflow/test
120120
[git-url]: https://gitter.im/postcss/postcss
121121
[npm-url]: https://www.npmjs.com/package/postcss-focus-within
122122

123123
[focus-within polyfill]: https://github.com/jsxtools/focus-within
124124
[Gulp PostCSS]: https://github.com/postcss/gulp-postcss
125125
[Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
126126
[PostCSS]: https://github.com/postcss/postcss
127-
[PostCSS Focus Within]: https://github.com/csstools/postcss-focus-within
127+
[PostCSS Focus Within]: https://github.com/csstools/postcss-plugins/tree/main/postcss-focus-within
128128
[PostCSS Loader]: https://github.com/postcss/postcss-loader
129129
[Selectors Level 4 specification]: https://www.w3.org/TR/selectors-4/#the-focus-within-pseudo

plugins/postcss-focus-within/package.json

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,96 +5,40 @@
55
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
66
"license": "CC0-1.0",
77
"repository": "csstools/postcss-focus-within",
8-
"homepage": "https://github.com/csstools/postcss-focus-within#readme",
9-
"bugs": "https://github.com/csstools/postcss-focus-within/issues",
8+
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-focus-within#readme",
9+
"bugs": "https://github.com/csstools/postcss-plugins/issues",
1010
"main": "dist/index.cjs",
1111
"module": "dist/index.mjs",
12-
"types": "./dist/index.d.ts",
13-
"exports": {
14-
".": {
15-
"import": "./dist/index.mjs",
16-
"require": "./dist/index.cjs",
17-
"types": "./dist/index.d.ts"
18-
}
19-
},
12+
"types": "dist/index.d.ts",
2013
"files": [
14+
"CHANGELOG.md",
15+
"INSTALL.md",
16+
"LICENSE.md",
17+
"README.md",
2118
"dist"
2219
],
20+
"bin": {
21+
"postcss-focus-within": "dist/cli.mjs"
22+
},
2323
"scripts": {
24-
"build": "npx rollup -c .rollup.js",
25-
"build:watch": "npx rollup -c .rollup.js --watch",
26-
"lint": "npx eslint --cache src",
27-
"lint:fix": "npx eslint --cache --fix",
28-
"prepublishOnly": "npm test",
29-
"pretest": "npm install && npm run build",
30-
"test": "npm run lint && npm run tape",
31-
"tape": "npx postcss-tape"
24+
"prepublishOnly": "npm run build && npm run test",
25+
"lint": "eslint src/**/*.ts",
26+
"test": "postcss-tape",
27+
"build": "rollup -c ../../rollup/default.ts.js",
28+
"stryker": "stryker run --logLevel error"
3229
},
3330
"engines": {
34-
"node": ">=12"
31+
"node": "^12 || ^14 || >=16"
3532
},
36-
"peerDependencies": {
37-
"postcss": "^8.3"
33+
"dependencies": {
34+
"postcss-selector-parser": "6.0.6"
3835
},
3936
"devDependencies": {
40-
"@babel/core": "7.15.5",
41-
"@babel/preset-env": "7.15.6",
42-
"eslint": "7.32.0",
4337
"postcss": "8.3.6",
44-
"postcss-tape": "6.0.1",
45-
"pre-commit": "1.2.2",
46-
"rollup": "2.57.0",
47-
"rollup-plugin-babel": "4.4.0",
48-
"rollup-plugin-copy": "^3.4.0"
38+
"postcss-tape": "6.0.1"
4939
},
50-
"babel": {
51-
"presets": [
52-
[
53-
"@babel/env",
54-
{
55-
"targets": "maintained node versions"
56-
}
57-
]
58-
]
59-
},
60-
"eslintConfig": {
61-
"env": {
62-
"es6": true,
63-
"node": true
64-
},
65-
"extends": "eslint:recommended",
66-
"parserOptions": {
67-
"ecmaVersion": 2020,
68-
"sourceType": "module"
69-
}
70-
},
71-
"rollup": {
72-
"input": "src/index.js",
73-
"plugins": [
74-
"rollup-plugin-babel",
75-
[
76-
"rollup-plugin-copy",
77-
{
78-
"targets": [
79-
{
80-
"src": "./src/index.d.ts",
81-
"dest": "./dist"
82-
}
83-
]
84-
}
85-
]
86-
],
87-
"output": [
88-
{
89-
"exports": "default",
90-
"file": "dist/index.cjs",
91-
"format": "cjs"
92-
},
93-
{
94-
"file": "dist/index.mjs",
95-
"format": "esm"
96-
}
97-
]
40+
"peerDependencies": {
41+
"postcss": "^8.3"
9842
},
9943
"keywords": [
10044
"postcss",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import plugin from './index';
2+
import { cli, helpTextLogger } from '@csstools/base-cli';
3+
4+
cli(
5+
plugin,
6+
['preserve', 'replaceWith'],
7+
helpTextLogger(
8+
'postcss-focus-within',
9+
'PostCSS Focus Within',
10+
'Lets you use the `:focus-within` pseudo-class in CSS, following the Selectors Level 4 specification.',
11+
{
12+
preserve: true,
13+
replaceWith: '.focus-within',
14+
},
15+
),
16+
);

plugins/postcss-focus-within/src/index.d.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

plugins/postcss-focus-within/src/index.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import parser from 'postcss-selector-parser';
2+
import type { PluginCreator } from 'postcss';
3+
4+
const selectorRegExp = /(?<!\\):focus-within([^\w-]|$)/gi;
5+
6+
const creator: PluginCreator<{ preserve?: boolean, replaceWith?: string }> = (opts?: { preserve?: boolean, replaceWith?: string }) => {
7+
const replaceWith = String(Object(opts).replaceWith || '[focus-within]');
8+
const preserve = Boolean('preserve' in Object(opts) ? opts.preserve : true);
9+
const replacementAST = parser().astSync(replaceWith);
10+
11+
return {
12+
postcssPlugin: 'postcss-focus-within',
13+
Rule: (rule)=> {
14+
if (!selectorRegExp.test(rule.selector)) {
15+
return;
16+
}
17+
18+
const modifiedSelectorAST = parser((selectors) => {
19+
selectors.walkPseudos((pseudo) => {
20+
if (pseudo.value !== ':focus-within') {
21+
return;
22+
}
23+
24+
pseudo.replaceWith(replacementAST.clone({}));
25+
});
26+
}).processSync(rule.selector);
27+
28+
const clone = rule.clone({ selector: String(modifiedSelectorAST) });
29+
30+
if (preserve) {
31+
rule.before(clone);
32+
} else {
33+
rule.replaceWith(clone);
34+
}
35+
},
36+
};
37+
};
38+
39+
creator.postcss = true;
40+
41+
export default creator;
42+

0 commit comments

Comments
 (0)