ShtukbaseStandard
файл пакета→ .shtukbase/packages/design-scales/checks/spacing.ts

checks/spacing.ts

44 строки, 2.8 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // SCL-R01. Spacing sits on the scale: every margin, padding and gap written as a length is a multiple of the base
  2. // step (4 px); small values (below 16 px) may use the fine step (2 px). rem counts as 16 px.
  3. // Looked at: CSS declarations (margin*, padding*, gap, row-gap, column-gap, scroll-margin*, scroll-padding*) and
  4. // spacing custom properties (--space*, --ds-spacing-4, --gap*), Tailwind classes with arbitrary values (p-[13px],
  5. // gap-[10px], -mt-[3px]) and inline styles in TSX/JSX (padding: 13). Named Tailwind steps (p-3, gap-1.5) are on
  6. // the scale by construction. var(), calc(), % and auto are skipped. Below 4 px a value is an optical nudge, not
  7. // spacing: any is fine.
  8. import { report } from '../../design-base/lib/files.ts';
  9. import { classes, config, cssDecls, fmt, lengths, styleValues, utility } from './scaleslib.ts';
  10. export const RULE = { id: 'scl-r01', title: 'Spacing on a 4 px scale' };
  11. const CSS_PROP = /^(margin|padding|scroll-margin|scroll-padding)(-(top|right|bottom|left|inline|block|inline-start|inline-end|block-start|block-end))?$|^(gap|row-gap|column-gap|grid-gap)$/;
  12. const TOKEN = /^--([\w-]*-)?(space|spacing|gap|gutter)(-[\w-]*)?$/;
  13. const TW = /^(p|px|py|pt|pr|pb|pl|ps|pe|m|mx|my|mt|mr|mb|ml|ms|me|gap|gap-x|gap-y|space-x|space-y|scroll-m|scroll-p|scroll-mx|scroll-my|scroll-px|scroll-py)-\[([^\]]+)\]$/;
  14. const STYLE = /margin|marginTop|marginRight|marginBottom|marginLeft|marginInline|marginBlock|padding|paddingTop|paddingRight|paddingBottom|paddingLeft|paddingInline|paddingBlock|gap|rowGap|columnGap/;
  15. export function onScale(p: number, c: ReturnType<typeof config>): boolean {
  16. const a = Math.abs(p);
  17. const mult = (x: number, step: number) => Math.abs(x / step - Math.round(x / step)) < 1e-6;
  18. return a < 4 || mult(a, c.base) || (a < c.fineBelow && mult(a, c.fine));
  19. }
  20. export function check(root = process.cwd()): string[] {
  21. const c = config(root);
  22. const out: string[] = [];
  23. const say = (at: string, what: string, p: number) =>
  24. out.push(`${at}: spacing ${what} = ${fmt(p)} — not on the ${c.base} px scale (below ${c.fineBelow} px the step is ${c.fine}); nearest: ${fmt(Math.floor(p / c.base) * c.base)}, ${fmt(Math.ceil(p / c.base) * c.base)}`);
  25. for (const d of cssDecls(root)) {
  26. if (!CSS_PROP.test(d.prop) && !TOKEN.test(d.prop)) continue;
  27. for (const p of lengths(d.value)) if (p !== null && !onScale(p, c)) say(d.at, `${d.prop}: ${d.value}`, Math.abs(p));
  28. }
  29. for (const k of classes(root)) {
  30. const m = TW.exec(utility(k.cls));
  31. if (!m) continue;
  32. for (const p of lengths(m[2].replace(/_/g, ' '))) if (p !== null && !onScale(p, c)) say(k.at, k.cls, Math.abs(p));
  33. }
  34. for (const s of styleValues(STYLE, root)) {
  35. for (const p of lengths(s.value)) if (p !== null && !onScale(p, c)) say(s.at, `${s.prop}: ${s.value}`, Math.abs(p));
  36. }
  37. return out;
  38. }
  39. if (import.meta.main) report(check());