-
Notifications
You must be signed in to change notification settings - Fork 14
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
feat(documentation): Add pseudo-class to snapshots with postcss plugin #2092
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c10bc9e
feat(documentation): Add pseudo-class to snapshots
imagoiq ce6190f
Remove focused story arg as it's not working
imagoiq 12d9ae2
Merge branch 'main' into feat/1051-snapshot-pseudo-class
imagoiq e701fe0
Use postcss plugin
imagoiq a57faaf
Remove unused packages
imagoiq f1757bc
Fix sonar code smell
imagoiq 4a54705
Fix sonar code smell
imagoiq 6c7d8b7
Fix select snapshot
imagoiq a31fe60
Add table-hover snapshots
imagoiq 34fe8fc
Add more blacklisted pseudo-class
imagoiq 7a0d1fd
Remove unused config
imagoiq 9bd1fb1
Delete pnpm-lock.yaml
imagoiq 3a4f39f
Fix pnpm lock
imagoiq 369e5df
Merge branch 'main' into feat/1051-snapshot-pseudo-class-v2
imagoiq 764a01b
Fix pnpm lock
imagoiq 81fe38e
Merge branch 'main' into feat/1051-snapshot-pseudo-class-v2
imagoiq 22b41c2
Merge branch 'main' into feat/1051-snapshot-pseudo-class-v2
imagoiq 0b87300
Merge branch 'main' into feat/1051-snapshot-pseudo-class-v2
imagoiq 200addf
Add button-group snapshots pseudoclass variants
imagoiq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/** | ||
* PostCSS plugin to automatically add in companion classes where pseudo-selectors are used. | ||
* This allows you to add the class name to force the styling of a pseudo-selector, which can be really helpful for testing or being able to concretely reach all style states. | ||
* @param {array} [options.blacklist] - Define elements to be ignored | ||
* @param {array} [options.restrictTo] - Create classes for a restricted list of selectors. e.g. [':nth-child', 'hover'] | ||
* @param {boolean} [options.allCombinations=false] - When enabled output with all combinations of pseudo styles/pseudo classes. | ||
* @param {boolean} [options.preserveBeforeAfter=true] - When enabled output does not generate pseudo classes for `:before` and `:after`. | ||
* @param {string} [options.prefix='\\:'] - Define the pseudo-class class prefix. Default: ':' so the class name will be ':hover' for example. | ||
* @author giuseppeg <https://github.com/giuseppeg> | ||
* @author philippone <https://github.com/philippone> | ||
* @author michaeldfoley <https://github.com/michaeldfoley> | ||
* @see {@link https://github.com/giuseppeg/postcss-pseudo-classes} | ||
* @returns {{postcssPlugin: string, Once(*): void}} | ||
*/ | ||
const plugin = (options = {}) => { | ||
options.preserveBeforeAfter = options?.preserveBeforeAfter || true; | ||
|
||
// Backwards compatibility--we always by default ignored `:root`. | ||
const blacklist = { | ||
':root': true, | ||
':host': true, | ||
':host-context': true, | ||
':not': true, | ||
':is': true, | ||
':where': true, | ||
':has': true, | ||
':nth-child': true, | ||
':nth-last-child': true, | ||
':nth-last-of-type': true, | ||
':first-child': true, | ||
':last-child': true, | ||
':first': true, | ||
':last': true, | ||
':required': true, | ||
':scope': true, | ||
':target': true, | ||
':valid': true, | ||
':user-valid': true, | ||
':user-invalid': true, | ||
':placeholder-shown': true, | ||
}; | ||
|
||
const prefix = options?.prefix || '\\:'; | ||
|
||
(options?.blacklist || []).forEach(function (blacklistItem) { | ||
blacklist[blacklistItem] = true; | ||
}); | ||
|
||
let restrictTo; | ||
|
||
if (Array.isArray(options.restrictTo) && options.restrictTo.length) { | ||
restrictTo = options.restrictTo.reduce(function (target, pseudoClass) { | ||
const finalClass = | ||
(pseudoClass.charAt(0) === ':' ? '' : ':') + pseudoClass.replace(/\(.*/g, ''); | ||
if (!Object.hasOwn(target, finalClass)) { | ||
target[finalClass] = true; | ||
} | ||
return target; | ||
}, {}); | ||
} | ||
|
||
return { | ||
postcssPlugin: 'postcss-pseudo-classes', | ||
Once(css) { | ||
css.walkRules(function (rule) { | ||
let combinations; | ||
|
||
rule.selectors.forEach(function (selector) { | ||
// Ignore some popular things that are never useful | ||
if (blacklist[selector]) { | ||
return; | ||
} | ||
|
||
const selectorParts = selector.split(' '); | ||
const pseudoedSelectorParts = []; | ||
|
||
selectorParts.forEach(function (selectorPart, index) { | ||
const pseudos = selectorPart.match(/::?([^:]+)/g); | ||
|
||
if (!pseudos) { | ||
if (options.allCombinations) { | ||
pseudoedSelectorParts[index] = [selectorPart]; | ||
} else { | ||
pseudoedSelectorParts.push(selectorPart); | ||
} | ||
return; | ||
} | ||
|
||
const baseSelector = selectorPart.substr( | ||
0, | ||
selectorPart.length - pseudos.join('').length, | ||
); | ||
|
||
const classPseudos = pseudos.map(function (pseudo) { | ||
const pseudoToCheck = pseudo.replace(/\(.*/g, ''); | ||
// restrictTo a subset of pseudo classes | ||
if ( | ||
blacklist[pseudoToCheck] || | ||
pseudoToCheck.split('.').some(item => blacklist[item]) || | ||
pseudoToCheck.split('#').some(item => blacklist[item]) || | ||
(restrictTo && !restrictTo[pseudoToCheck]) | ||
) { | ||
return pseudo; | ||
} | ||
|
||
// Ignore pseudo-elements! | ||
if (pseudo.match(/^::/)) { | ||
return pseudo; | ||
} | ||
|
||
// Ignore ':before' and ':after' | ||
if (options.preserveBeforeAfter && [':before', ':after'].indexOf(pseudo) !== -1) { | ||
return pseudo; | ||
} | ||
|
||
// Kill the colon | ||
pseudo = pseudo.substr(1); | ||
|
||
// check if pseudo is css function with opening and closing parentheses (.+) | ||
if (pseudo.match(/\(.+\)/)) { | ||
// Replace left and right parens | ||
pseudo = pseudo.replace(/\(/g, '\\('); | ||
|
||
pseudo = pseudo.replace(/\)/g, '\\)'); | ||
|
||
} else { | ||
// Replace left and right parens | ||
pseudo = pseudo.replace(/\(/g, '('); | ||
|
||
pseudo = pseudo.replace(/\)/g, ')'); | ||
|
||
} | ||
|
||
return '.' + prefix + pseudo; | ||
}); | ||
|
||
// Add all combinations of pseudo selectors/pseudo styles given a | ||
// selector with multiple pseudo styles. | ||
if (options.allCombinations) { | ||
combinations = createCombinations(pseudos, classPseudos); | ||
pseudoedSelectorParts[index] = []; | ||
|
||
combinations.forEach(function (combination) { | ||
pseudoedSelectorParts[index].push(baseSelector + combination); | ||
}); | ||
} else { | ||
pseudoedSelectorParts.push(baseSelector + classPseudos.join('')); | ||
} | ||
}); | ||
|
||
if (options.allCombinations) { | ||
const serialCombinations = createSerialCombinations( | ||
pseudoedSelectorParts, | ||
appendWithSpace, | ||
); | ||
|
||
serialCombinations.forEach(function (combination) { | ||
addSelector(combination); | ||
}); | ||
} else { | ||
addSelector(pseudoedSelectorParts.join(' ')); | ||
} | ||
|
||
function addSelector(newSelector) { | ||
if (newSelector && newSelector !== selector) { | ||
rule.selector += ',\n' + newSelector; | ||
} | ||
} | ||
}); | ||
}); | ||
}, | ||
}; | ||
}; | ||
|
||
plugin.postcss = true; | ||
|
||
module.exports = plugin; | ||
|
||
// a.length === b.length | ||
function createCombinations(a, b) { | ||
let combinations = ['']; | ||
let newCombinations; | ||
for (let i = 0, len = a.length; i < len; i += 1) { | ||
newCombinations = []; | ||
combinations.forEach(function (combination) { | ||
newCombinations.push(combination + a[i]); | ||
// Don't repeat work. | ||
if (a[i] !== b[i]) { | ||
newCombinations.push(combination + b[i]); | ||
} | ||
}); | ||
combinations = newCombinations; | ||
} | ||
return combinations; | ||
} | ||
|
||
// arr = [[list of 1st el], [list of 2nd el] ... etc] | ||
function createSerialCombinations(arr, fn) { | ||
let combinations = ['']; | ||
let newCombinations; | ||
arr.forEach(function (elements) { | ||
newCombinations = []; | ||
elements.forEach(function (element) { | ||
combinations.forEach(function (combination) { | ||
newCombinations.push(fn(combination, element)); | ||
}); | ||
}); | ||
combinations = newCombinations; | ||
}); | ||
return combinations; | ||
} | ||
|
||
function appendWithSpace(a, b) { | ||
if (a) { | ||
a += ' '; | ||
} | ||
return a + b; | ||
} |
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,7 @@ | ||
const postcssPseudoClasses = require('./postcss-pseudo-classes.js'); | ||
|
||
module.exports = () => { | ||
return { | ||
plugins: [postcssPseudoClasses], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't able to change the Vite config to use this plugin like vitejs/vite#8693 |
||
}; | ||
}; |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we should publish it as new package perhaps?