diff --git a/packages/stylelint-config/README.md b/packages/stylelint-config/README.md index b6083961..acf1fe9a 100644 --- a/packages/stylelint-config/README.md +++ b/packages/stylelint-config/README.md @@ -50,7 +50,8 @@ There are additional configs for specific language features and strict mode: | Config | Description | |--------|-------------| | @trigen/stylelint-config/logical | Rules with CSS logical properties support. | -| @trigen/stylelint-config/strict | Strict rules for enhanced code quality. | +| @trigen/stylelint-config/strict | Strict rules for enhanced code quality: colors, fonts, layers and lengths through tokens. | +| @trigen/stylelint-config/flat-layers | Rules for the files of Flat Layers Design: tokens, breakpoints, container names, mixins and global styles each in their own file (`variables.css`, `media.css`, `containers.module.css`, `mixins.module.css`, `globals.css`, `styles.css`). | Example: diff --git a/packages/stylelint-config/src/flat-layers.js b/packages/stylelint-config/src/flat-layers.js new file mode 100644 index 00000000..f8e6d45e --- /dev/null +++ b/packages/stylelint-config/src/flat-layers.js @@ -0,0 +1,5 @@ +/** + * Export Flat Layers Design subconfig + */ + +export { default } from './subconfigs/flat-layers.js' diff --git a/packages/stylelint-config/src/subconfigs/common.js b/packages/stylelint-config/src/subconfigs/common.js index 54048742..d8ba8217 100644 --- a/packages/stylelint-config/src/subconfigs/common.js +++ b/packages/stylelint-config/src/subconfigs/common.js @@ -123,7 +123,13 @@ export default { 'color-function-notation': 'legacy', 'color-named': 'always-where-possible', // Length - 'length-zero-no-unit': true, + // A custom property keeps its unit: `0px` in `calc()` is a length, a bare `0` is not + 'length-zero-no-unit': [ + true, + { + ignore: ['custom-properties'] + } + ], // Font weight 'font-weight-notation': 'named-where-possible', // Function diff --git a/packages/stylelint-config/src/subconfigs/flat-layers.js b/packages/stylelint-config/src/subconfigs/flat-layers.js new file mode 100644 index 00000000..e4ee1257 --- /dev/null +++ b/packages/stylelint-config/src/subconfigs/flat-layers.js @@ -0,0 +1,131 @@ +/** + * Flat Layers Design config + */ + +import { + strictCustomPropertyMatcher, + strictMediaQueryMatcher +} from './matchers.js' + +// The files the flat-layers-design and css-containers skills keep the tokens, the breakpoints, +// the mixins, the container names and the global styles in +const variablesFiles = ['**/variables.css'] +const mediaFiles = ['**/media.css'] +const mixinsFiles = ['**/mixins.module.css'] +const containersFiles = ['**/containers.module.css'] +const globalFiles = [ + '**/styles.css', + '**/globals.css', + ...variablesFiles, + ...mixinsFiles +] +const mediaFeatures = [ + 'width', + 'min-width', + 'max-width', + 'height', + 'min-height', + 'max-height', + 'hover', + 'any-hover', + 'pointer', + 'any-pointer', + 'prefers-reduced-motion', + 'prefers-contrast', + 'forced-colors', + 'orientation', + 'aspect-ratio', + 'resolution' +] + +export default { + rules: { + // Breakpoints and features are custom media of `media.css`, and the theme is the tokens' or, + // for a picture drawn per theme, the mixins' + 'media-feature-name-disallowed-list': [...mediaFeatures, 'prefers-color-scheme'], + 'at-rule-disallowed-list': ['custom-media'], + 'custom-media-pattern': [ + strictMediaQueryMatcher, + { + message: 'Expected custom media query name to be in camelCase and start with "screen" (e.g. "--screenSmUp", "--screenReducedMotion")' + } + ], + // A container's name is a token of `containers.module.css`: an empty list takes no value + 'declaration-property-value-allowed-list': { + '/^container(-name)?$/': [] + }, + // A module styles its own classes, and the root only while one of them stands, by `:has()`; + // the chosen theme is read by the mixins alone + 'selector-disallowed-list': [ + [ + '/^:global(?!\\(html\\):has\\()/', + '/^(:root|html|body)\\b/', + '/data-theme/' + ], + { + splitList: true + } + ], + // Only a mixin, a utility, has to win over what it stands beside + 'declaration-no-important': true, + // Colors and easings are tokens + 'color-no-hex': true, + 'function-disallowed-list': [ + 'rgb', + 'rgba', + 'hsl', + 'hsla', + 'hwb', + 'lab', + 'lch', + 'oklab', + 'oklch', + 'color', + 'color-mix', + 'cubic-bezier', + 'steps' + ] + }, + overrides: [ + { + files: variablesFiles, + rules: { + 'color-no-hex': null, + 'function-disallowed-list': null, + // A token's name starts with its type: `--colorText`, `--sizeSpace4` + 'custom-property-pattern': [ + strictCustomPropertyMatcher, + { + message: 'Expected token name to be in camelCase and start with its type (e.g. "--colorPrimary", "--sizeFont")' + } + ] + } + }, + { + files: mediaFiles, + rules: { + 'media-feature-name-disallowed-list': null, + 'at-rule-disallowed-list': null + } + }, + { + files: mixinsFiles, + rules: { + 'media-feature-name-disallowed-list': mediaFeatures, + 'declaration-no-important': null + } + }, + { + files: containersFiles, + rules: { + 'declaration-property-value-allowed-list': null + } + }, + { + files: globalFiles, + rules: { + 'selector-disallowed-list': null + } + } + ] +} diff --git a/packages/stylelint-config/src/subconfigs/matchers.js b/packages/stylelint-config/src/subconfigs/matchers.js index ba027daf..f80d310a 100644 --- a/packages/stylelint-config/src/subconfigs/matchers.js +++ b/packages/stylelint-config/src/subconfigs/matchers.js @@ -6,6 +6,6 @@ export const pascalCaseMatcher = /^([A-Z][a-z\d]*)+$/ export const camelCaseMatcher = /^[a-z\d]+([A-Z][a-z\d]*)*$/ -export const strictMediaQueryMatcher = /^media([A-Z][a-z\d]*)+$/ +export const strictMediaQueryMatcher = /^screen([A-Z][a-z\d]*)+$/ -export const strictCustomPropertyMatcher = /^(size|color|duration|shadow|opacity|zIndex|font|animation)([A-Z][a-z\d]*)+$/ +export const strictCustomPropertyMatcher = /^(duration|opacity|color|size|shadow|font|fontWeight|easing|zIndex|transition)([A-Z][a-z\d]*)+$/ diff --git a/packages/stylelint-config/src/subconfigs/strict.js b/packages/stylelint-config/src/subconfigs/strict.js index df5c6ae7..ab0997e2 100644 --- a/packages/stylelint-config/src/subconfigs/strict.js +++ b/packages/stylelint-config/src/subconfigs/strict.js @@ -2,42 +2,111 @@ * Strict config */ -import { - strictCustomPropertyMatcher, - strictMediaQueryMatcher -} from './matchers.js' +const systemColors = '/^(AccentColor|AccentColorText|ActiveText|ButtonBorder|ButtonFace|ButtonText|Canvas|CanvasText|Field|FieldText|GrayText|Highlight|HighlightText|LinkText|Mark|MarkText|SelectedItem|SelectedItemText|VisitedText)$/' +const colorValues = [ + 'currentcolor', + 'currentColor', + 'transparent', + 'inherit', + 'none', + systemColors +] +// Values a length needs no token for +const lengthValues = [ + '0', + '1', + 'auto', + 'none', + 'inherit', + 'normal', + '/^-?\\d+%$/' +] +// A property here takes a token, a calc of tokens or a value that needs none +const strictValue = [ + [ + '/color$/', + 'fill', + 'stroke', + 'font-family', + 'font-weight', + 'z-index', + '/^padding/', + '/^margin/', + '/^(gap|row-gap|column-gap)$/', + '/^(inset|top|right|bottom|left)(-|$)/', + '/radius$/', + 'font-size', + 'line-height', + 'outline-offset', + 'text-underline-offset', + 'text-decoration-thickness', + '/^scroll-(padding|margin)/' + ], + { + disableFix: true, + ignoreValues: { + '': lengthValues, + '/color$/': colorValues, + fill: colorValues, + stroke: colorValues, + 'font-family': ['inherit'], + 'font-weight': ['inherit'], + // A local order inside a component: the layers across components are tokens + 'z-index': ['auto', '/^-?[0-2]$/'] + } + } +] export default { plugins: ['stylelint-declaration-strict-value', 'stylelint-plugin-a11y'], rules: { - 'scale-unlimited/declaration-strict-value': [ - ['font-family', '/color([^A-Z]|$)/'], - { - disableFix: true, - ignoreKeywords: [ - 'currentColor', - 'transparent', - 'inherit' - ] - } + 'scale-unlimited/declaration-strict-value': strictValue, + 'declaration-property-value-disallowed-list': { + // A color reads a role, which is a `light-dark()` pair of the tokens + '/^(?!--)/': ['/light-dark\\(/'], + // No pulls: a box stands where its box stands + '/^margin/': [ + '/\\*\\s*-1\\b/', + '/-1\\s*\\*/', + '/(^|[\\s(,])-(\\d|var)/' + ] + }, + 'property-disallowed-list': [ + // The shorthand alone: the logical autofix turns `overflow-y` into `overflow-block`, which + // Safari 18 drops + 'overflow-x', + 'overflow-y', + 'overflow-block', + 'overflow-inline', + // A mixin is a class in the markup + 'composes' ], + 'rule-selector-property-disallowed-list': { + // The root keeps its scrollbar's room and its scroll anchoring + '/^(html|:root)\\b|:global\\(html\\)/': [ + 'overflow-anchor', + 'scrollbar-gutter', + 'overflow-x', + 'overflow-y', + 'overflow-block' + ], + // A block does not place itself: where it stands is its consumer's. Its margin is left out: + // a root resets the one its element has with `0`, which a list of properties cannot tell + '/^\\.root(?![\\w-])[^\\s>+~]*$/': [ + 'inset', + '/^inset-/', + 'top', + 'right', + 'bottom', + 'left', + 'z-index' + ] + }, 'a11y/line-height-is-vertical-rhythmed': [ true, { severity: 'warning' } - ], - 'custom-property-pattern': [ - strictCustomPropertyMatcher, - { - message: 'Expected custom property name to be in camelCase (e.g. "--colorPrimary", "--sizeFont")' - } - ], - 'custom-media-pattern': [ - strictMediaQueryMatcher, - { - message: 'Expected custom media query name to be in camelCase (e.g. "--mediaSmallScreen", "--mediaTabletAndUp")' - } ] } }