A platform for community discussion. Free, open, simple.
DEV: Exploring a token system (#38700)
Jordan Vidrinedff581427bf8
diff --git a/app/assets/stylesheets/admin/sidebar.scss b/app/assets/stylesheets/admin/sidebar.scssindex 9592ed9574c..ba65915590e 100644--- a/app/assets/stylesheets/admin/sidebar.scss+++ b/app/assets/stylesheets/admin/sidebar.scss@@ -1,15 +1,11 @@.admin-area .sidebar-wrapper .admin-panel {+ --d-sidebar-admin-background: var(--token-color-surface-hovered);+ --d-sidebar-active-background: var(--token-color-background-accent-subtler);+ --d-sidebar-highlight-background: var(--token-color-surface-pressed);+ --d-sidebar-link-color: var(--token-color-text-default);background-color: var(--d-sidebar-admin-background);-- --d-sidebar-highlight-background: var(--primary-200);- --d-sidebar-link-icon-color: var(--primary-600);-- .sidebar-section-header-text {- font-weight: bold;- color: var(--primary-very-high);- }}.admin-sidebar-nav-link__dot {- color: var(--tertiary-med-or-tertiary);+ color: var(--token-color-background-accent-subtler);}diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scssindex 8befcb94233..2d8fea87e3c 100644--- a/app/assets/stylesheets/common.scss+++ b/app/assets/stylesheets/common.scss@@ -1,4 +1,5 @@@import "common/font-variables";+@import "common/tokens";@import "vendor/normalize";@import "vendor/normalize-ext";@import "vendor/pikaday";diff --git a/app/assets/stylesheets/common/base/sidebar-custom-section.scss b/app/assets/stylesheets/common/base/sidebar-custom-section.scssindex 58a89eba31c..211571a04b8 100644--- a/app/assets/stylesheets/common/base/sidebar-custom-section.scss+++ b/app/assets/stylesheets/common/base/sidebar-custom-section.scss@@ -14,29 +14,6 @@@include unselectable;cursor: pointer;}-- .sidebar-section-wrapper.disabled {- a {- pointer-events: none;- }-- .sidebar-section-link-wrapper {- .sidebar-section-link-prefix.icon,- .sidebar-section-link {- background: none;- color: var(--primary-low-mid);- }-- .sidebar-section-link.drag {- font-weight: bold;- color: var(--primary-high);-- .sidebar-section-link-prefix.icon {- color: var(--primary-high);- }- }- }- }}.discourse-touch {diff --git a/app/assets/stylesheets/common/base/sidebar-section-link.scss b/app/assets/stylesheets/common/base/sidebar-section-link.scssindex 3363802d5d3..28e8da82385 100644--- a/app/assets/stylesheets/common/base/sidebar-section-link.scss+++ b/app/assets/stylesheets/common/base/sidebar-section-link.scss@@ -24,6 +24,7 @@.prefix-text {background: var(--d-sidebar-highlight-prefix-background);+ color: var(--d-sidebar-highlight-prefix-color);}.sidebar-section-link-suffix.icon.unread svg {@@ -36,7 +37,7 @@width: 100%;align-items: center;color: var(--d-sidebar-link-color);- border-radius: var(--d-border-radius);+ border-radius: var(--token-radius-normal);gap: var(--d-sidebar-section-link-prefix-margin-right);&:focus-visible,@@ -47,6 +48,7 @@.prefix-text {background: var(--d-sidebar-highlight-prefix-background);+ color: var(--d-sidebar-highlight-prefix-color);}.sidebar-section-link-suffix.icon.unread svg {@@ -81,17 +83,11 @@}&--muted {- opacity: 0.5;-- .sidebar-section-link-prefix.icon .d-icon {- color: var(--primary-medium);+ &:not(.active) .sidebar-section-link-prefix {+ opacity: 0.75;}- &.active {- .sidebar-section-link-prefix.icon .d-icon {- color: var(--primary-high);- }- }+ --d-sidebar-link-color: var(--token-color-text-subtlest);}.sidebar-section-link-content-badge {@@ -100,7 +96,7 @@width: auto;min-width: var(--font-down-1);padding-inline: var(--space-1);- font-weight: normal;+ font-weight: var(--token-font-weight-regular);flex-shrink: 0;text-align: center;max-width: 15ch;@@ -159,7 +155,7 @@.dropdown-menu__item .sidebar-section-link {height: var(--d-sidebar-row-height);padding-inline: var(--d-sidebar-row-horizontal-padding);- border-radius: var(--d-border-radius);+ border-radius: var(--token-radius-normal);}.--link-button {@@ -198,7 +194,7 @@&.image {img {- border-radius: 100%;+ border-radius: var(--token-radius-full);aspect-ratio: auto 20 / 20;width: calc(var(--d-sidebar-section-link-prefix-width) - 2px);height: calc(var(--d-sidebar-section-link-prefix-width) - 2px);@@ -222,7 +218,7 @@.prefix-badge {position: absolute;background-color: transparent;- border-radius: 50%;+ border-radius: var(--token-radius-full);padding: 2px 2px 3px;color: var(--d-sidebar-link-color);height: 0.5rem;@@ -248,7 +244,7 @@display: flex;align-items: center;justify-content: center;- border-radius: 100%;+ border-radius: var(--token-radius-full);background: var(--d-sidebar-prefix-background);color: var(--d-sidebar-prefix-color);width: calc(var(--d-sidebar-section-link-prefix-width) - 2px);@@ -270,12 +266,18 @@.discourse-no-touch & {&:hover {color: var(--d-sidebar-highlight-color);+ background: var(--d-sidebar-highlight-hover-background);++ svg {+ color: var(--d-sidebar-active-icon-color);+ }}}svg {font-size: var(--font-down-1);margin: 0;+ color: var(--d-sidebar-highlight-color);}}}diff --git a/app/assets/stylesheets/common/base/sidebar-section.scss b/app/assets/stylesheets/common/base/sidebar-section.scssindex e7a0eefb3af..3350e75982d 100644--- a/app/assets/stylesheets/common/base/sidebar-section.scss+++ b/app/assets/stylesheets/common/base/sidebar-section.scss@@ -46,7 +46,7 @@}&:focus {- outline: 2px solid var(--tertiary);+ outline: 2px solid var(--token-color-background-accent-bolder);}}}diff --git a/app/assets/stylesheets/common/base/sidebar.scss b/app/assets/stylesheets/common/base/sidebar.scssindex 110b844d7c0..5c83098c2de 100644--- a/app/assets/stylesheets/common/base/sidebar.scss+++ b/app/assets/stylesheets/common/base/sidebar.scss@@ -16,47 +16,47 @@--d-sidebar-row-height: 2.2em;--d-sidebar-animation-time: 0.25s;--d-sidebar-animation-ease: ease-in-out;- --d-sidebar-background: var(--secondary);- --d-sidebar-admin-background: var(--primary-very-low);+ --d-sidebar-background: var(--token-color-surface);// must be rgba for gradient- --d-sidebar-footer-fade: rgb(var(--secondary-rgb), 1);- --d-sidebar-header-color: var(--primary-medium);- --d-sidebar-header-icon-color: var(--primary-medium);- --d-sidebar-header-font-weight: bold;- --d-sidebar-section-border-color: var(--content-border-color);- --d-sidebar-border-color: var(--content-border-color);- --d-sidebar-link-color: var(--primary-high);- --d-sidebar-link-icon-color: var(--primary-500);- --d-sidebar-link-badge-color: var(--primary-700); // example: new count-+ --d-sidebar-footer-fade: rgb(from var(--token-color-surface) r g b / 1);+ --d-sidebar-header-color: var(--token-color-text-subtle);+ --d-sidebar-header-icon-color: var(--token-color-text-subtle);+ --d-sidebar-header-font-weight: var(--token-font-weight-semibold);+ --d-sidebar-section-border-color: var(--token-color-border-default);+ --d-sidebar-border-color: var(--token-color-border-default);+ --d-sidebar-link-color: var(--token-color-text-subtle);+ --d-sidebar-link-icon-color: var(--token-color-icon-subtle);+ --d-sidebar-link-badge-color: var(--token-color-text-subtle);++ // example: new count// example: chat participant count- --d-sidebar-prefix-background: var(--primary-low);+ --d-sidebar-prefix-background: var(--token-color-surface-pressed);--d-sidebar-prefix-color: var(--d-sidebar-link-color);// example: unread dot- --d-sidebar-suffix-color: var(--tertiary-med-or-tertiary);+ --d-sidebar-suffix-color: var(--token-color-background-accent-bolder);// highlight applies to both hover and focus states- --d-sidebar-highlight-background: var(--d-hover);- --d-sidebar-highlight-color: var(--primary-high);- --d-sidebar-highlight-prefix-background: var(--primary-300);- --d-sidebar-highlight-prefix-color: var(--d-sidebar-highlight-color);- --d-sidebar-highlight-suffix-color: var(--tertiary-med-or-tertiary);- --d-sidebar-highlight-hover-background: var(- --primary-medium- ); // example: hovering a button within a highlighted section-- --d-sidebar-highlight-hover-icon: var(- --primary-very-low- ); // example: hovering a button within a highlighted section-- --d-sidebar-active-background: var(--d-selected);- --d-sidebar-active-color: var(--d-sidebar-link-color);- --d-sidebar-active-icon-color: var(--d-sidebar-link-color);- --d-sidebar-active-prefix-background: var(--primary-200);- --d-sidebar-active-suffix-color: var(--tertiary-med-or-tertiary);- --d-sidebar-active-font-weight: bold; // example: active section link+ --d-sidebar-highlight-background: var(--token-color-surface-hovered);+ --d-sidebar-highlight-color: var(--token-color-text-default);+ --d-sidebar-highlight-prefix-background: var(--token-color-surface-pressed);+ --d-sidebar-highlight-prefix-color: var(--d-sidebar-link-color);+ --d-sidebar-highlight-suffix-color: var(--d-sidebar-suffix-color);+ --d-sidebar-highlight-hover-background: var(--token-color-surface-pressed);++ // example: hovering a button within a highlighted section+ --d-sidebar-highlight-hover-icon: var(--d-sidebar-link-color);++ // example: hovering a button within a highlighted section+ --d-sidebar-active-background: var(--token-color-surface-selected);+ --d-sidebar-active-color: var(--token-color-text-default);+ --d-sidebar-active-icon-color: var(--token-color-icon-default);+ --d-sidebar-active-prefix-background: var(--token-color-surface-hovered);+ --d-sidebar-active-suffix-color: var(--d-sidebar-suffix-color);+ --d-sidebar-active-font-weight: var(+ --token-font-weight-semibold+ ); // example: active section link@include viewport.until(sm) {--d-sidebar-row-height: 2.4em;@@ -123,7 +123,7 @@// custom scrollbar styling--scrollbarBg: transparent;- --scrollbarThumbBg: var(--primary-low);+ --scrollbarThumbBg: var(--token-color-surface-hovered);--scrollbarWidth: var(--space-2);scrollbar-color: transparent var(--scrollbarBg);transition: scrollbar-color 0.25s ease-in-out;@@ -174,7 +174,7 @@}.texts .name {- font-weight: normal;+ font-weight: var(--token-font-weight-regular);}}}@@ -187,7 +187,7 @@width: 100%;justify-content: center;align-items: center;- color: var(--primary-medium);+ color: var(--token-color-icon-subtlest);align-self: center;margin-left: auto;margin-right: auto;@@ -248,11 +248,11 @@}&.drag-above {- border-top: 2px solid var(--tertiary);+ border-top: 2px solid var(--token-color-border-accent);}&.drag-below {- border-bottom: 2px solid var(--tertiary);+ border-bottom: 2px solid var(--token-color-border-accent);}.link-icon {@@ -276,26 +276,6 @@padding-right: 0.5rem;}- .btn-flat.add-link {- margin-top: 0.5em;- margin-left: -0.5em;-- &:active,- &:focus {- background: none;- }-- svg {- color: var(--tertiary);- width: 0.75em;- height: 0.75em;- }-- &:hover svg {- color: var(--tertiary-hover);- }- }-.d-modal__footer {display: grid;grid-template-columns: auto 1fr auto;@@ -340,32 +320,15 @@min-width: 220px;}- .reset-link {- margin-right: 0;-- .discourse-no-touch & {- &:hover {- .d-icon {- color: var(--tertiary-hover);- }- }- }-- .d-icon {- font-size: var(--font-down-1);- color: var(--tertiary);- }- }-.delete-link {.d-icon {- color: var(--primary-medium);+ color: var(--token-color-text-subtle);}.discourse-no-touch & {&:hover {.d-icon {- color: var(--danger);+ color: var(--token-color-icon-danger);}}}@@ -408,7 +371,7 @@var(--d-sidebar-row-horizontal-padding);&__title {- font-weight: bold;+ font-weight: var(--token-font-weight-bold);}}@@ -418,12 +381,12 @@&__input-container {display: flex;align-items: center;- border: 1px solid var(--primary-400);- background: var(--d-input-bg-color);+ border: var(--token-border-width) solid var(--token-color-border-input);+ background: var(--token-color-background-input);box-sizing: border-box;margin-top: 0.5em;margin-bottom: 0.5em;- border-radius: var(--d-input-border-radius);+ border-radius: var(--token-radius-medium);overflow: hidden;padding-inline: var(--d-sidebar-row-horizontal-padding);gap: var(--d-sidebar-section-link-prefix-margin-right);@@ -433,16 +396,12 @@flex-shrink: 0;font-size: var(--d-sidebar-section-link-icon-size);border: 0;- background: var(--d-input-bg-color);+ background: var(--token-color-background-input);width: var(--d-sidebar-section-link-prefix-width);padding: 0;&:hover {- background: var(--d-input-bg-color) !important;- }-- .d-icon:hover {- color: var(--primary-high);+ background: transparent !important;}}@@ -460,11 +419,10 @@}&__shortcut-hint {- background-color: rgb(var(--tertiary-rgb), 0.1);min-width: 26px;padding: 0.25em 0.5em;font-size: var(--font-down-3);- color: var(--primary-medium);+ color: var(--d-sidebar-link-color);}}diff --git a/app/assets/stylesheets/common/tokens.scss b/app/assets/stylesheets/common/tokens.scssnew file mode 100644index 00000000000..572c4d7be8a--- /dev/null+++ b/app/assets/stylesheets/common/tokens.scss@@ -0,0 +1,64 @@+:root {+ // Text+ --token-color-text-default: var(--primary-900);+ --token-color-text-subtle: var(--primary-800);+ --token-color-text-subtlest: var(--primary-600);+ --token-color-text-accent: light-dark(+ var(--tertiary-800),+ var(--tertiary-300)+ );+ --token-color-text-accent-bolder: light-dark(+ var(--tertiary-900),+ var(--tertiary-200)+ );++ // for use on "bold" backgrounds+ --token-color-text-inverse: light-dark(var(--secondary), var(--primary));++ // Icon+ --token-color-icon-default: var(--primary-800);+ --token-color-icon-subtle: var(--primary-500);+ --token-color-icon-subtlest: var(--primary-400);+ --token-color-icon-danger: var(--danger);++ // Border+ --token-color-border-input: var(--primary-300);+ --token-color-border-default: var(--primary-low);+ --token-color-border-focused: var(--tertiary);+ --token-color-border-accent: var(--tertiary-400);++ // Backgrounds+ // Use as the primary background for the UI.+ --token-color-surface: var(--secondary);+ --token-color-surface-hovered: var(--primary-100);+ --token-color-surface-pressed: var(--primary-200);+ --token-color-background-input: var(--secondary);+ --token-color-surface-selected: var(--d-selected);++ // Accents+ --token-color-background-accent-subtlest: var(--tertiary-200);+ --token-color-background-accent-subtlest-hovered: var(--tertiary-300);+ --token-color-background-accent-subtlest-pressed: var(--tertiary-400);+ --token-color-background-accent-subtler: var(--tertiary-400);+ --token-color-background-accent-subtler-hovered: var(--tertiary-500);+ --token-color-background-accent-subtler-pressed: var(--tertiary-600);+ --token-color-background-accent-subtle: var(--tertiary-600);+ --token-color-background-accent-subtle-hovered: var(--tertiary-700);+ --token-color-background-accent-subtle-pressed: var(--tertiary-800);+ --token-color-background-accent-bolder: var(--tertiary);+ --token-color-background-accent-bolder-hovered: var(--tertiary-high);+ --token-color-background-accent-bolder-pressed: var(--tertiary-very-high);++ // Sizing+ --token-border-width: 1px;+ --token-radius-small: 2px;+ --token-radius-normal: 4px;+ --token-radius-large: 8px;+ --token-radius-full: 9999px;++ // font weight+ --token-font-weight-bold: 650;+ --token-font-weight-semibold: 600;+ --token-font-weight-medium: 500;+ --token-font-weight-regular: normal;+}diff --git a/stylelint-rules/no-core-variables.mjs b/stylelint-rules/no-core-variables.mjsnew file mode 100644index 00000000000..c0fd4638f87--- /dev/null+++ b/stylelint-rules/no-core-variables.mjs@@ -0,0 +1,49 @@+import stylelint from "stylelint";++const blocked = [+ /--primary(-.*)?/,+ /--secondary(-.*)?/,+ /--tertiary(-.*)?/,+ /--quarternary(-.*)?/,+];++const varRegex = /var\(\s*(--[^,)]+)/g;++export default stylelint.createPlugin(+ "discourse/no-core-color-variables",+ (primaryOption) => {+ return (root, result) => {+ if (!primaryOption) {+ return;+ }++ root.walkDecls((decl) => {+ const value = decl.value;+ if (!value.includes("var(")) {+ return;+ }++ let match;+ while ((match = varRegex.exec(value)) !== null) {+ const varName = match[1].trim();++ for (const pattern of blocked) {+ if (pattern.test(varName)) {+ stylelint.utils.report({+ message:+ "Do not use core color variables directly. Use var(--token-*) design tokens instead",+ node: decl,+ result,+ ruleName: "discourse/no-core-color-variables",+ word: varName,+ });+ break;+ }+ }+ }++ varRegex.lastIndex = 0;+ });+ };+ }+);diff --git a/stylelint-rules/require-design-tokens.mjs b/stylelint-rules/require-design-tokens.mjsnew file mode 100644index 00000000000..09f0f2c75de--- /dev/null+++ b/stylelint-rules/require-design-tokens.mjs@@ -0,0 +1,144 @@+import stylelint from "stylelint";++const ruleName = "discourse/require-design-tokens";++// Properties that must use --token-* variables instead of hardcoded values.+// Each entry maps a CSS property to the token prefix(es) that should be used.+const enforcedProperties = {+ // Colors — must use token vars, not raw colors+ color: "--token-color-text-* or --token-color-icon-*",+ "background-color": "--token-color-surface-* or --token-color-background-*",+ "border-color": "--token-color-border-*",++ // Font weight — must use --token-font-weight-*+ "font-weight": "--token-font-weight-*",++ // Border radius — must use --token-radius-*+ "border-radius": "--token-radius-*",+};++// Values that are always allowed (inherit, initial, transparent, currentColor, etc.)+const allowedGlobals = new Set([+ "inherit",+ "initial",+ "unset",+ "revert",+ "currentcolor",+ "transparent",+ "none",+ "0",+]);++// font-weight keywords that should use tokens instead+const fontWeightKeywords = new Set([+ "bold",+ "bolder",+ "lighter",+ "normal",+ "100",+ "200",+ "300",+ "400",+ "500",+ "600",+ "650",+ "700",+ "800",+ "900",+]);++function isTokenVar(value) {+ return /var\(\s*--token-/.test(value);+}++function containsRawColor(value) {+ // hex colors+ if (/#[0-9a-fA-F]{3,8}\b/.test(value)) {+ return true;+ }+ // rgb/rgba/hsl/hsla/oklch/lab/lch functions+ if (/\b(rgb|rgba|hsl|hsla|oklch|lab|lch|hwb|oklab)\s*\(/.test(value)) {+ return true;+ }+ // named colors (common ones — not exhaustive but covers the usual suspects)+ const namedColors =+ /\b(red|blue|green|yellow|orange|purple|pink|white|black|grey|gray|cyan|magenta|teal|navy|maroon|lime|olive|aqua|fuchsia|silver|indigo|violet|coral|salmon|tomato|gold|khaki|plum|orchid|sienna|tan|wheat|beige|ivory|linen|crimson|firebrick|darkred|darkblue|darkgreen|brown)\b/i;+ if (namedColors.test(value)) {+ return true;+ }+ return false;+}++export default stylelint.createPlugin(ruleName, (primaryOption) => {+ return (root, result) => {+ if (!primaryOption) {+ return;+ }++ root.walkDecls((decl) => {+ const prop = decl.prop.toLowerCase();+ const value = decl.value.trim().toLowerCase();++ // Skip custom property definitions (--token-* declarations themselves)+ if (prop.startsWith("--")) {+ return;+ }++ // Skip if already using a token variable+ if (isTokenVar(decl.value)) {+ return;+ }++ // Skip globally allowed values+ if (allowedGlobals.has(value)) {+ return;+ }++ const tokenSuggestion = enforcedProperties[prop];++ if (!tokenSuggestion) {+ return;+ }++ // Font weight check+ if (prop === "font-weight") {+ if (fontWeightKeywords.has(value)) {+ stylelint.utils.report({+ message: `Avoid hardcoded "${decl.value}" for ${prop}. Use a design token: ${tokenSuggestion}`,+ node: decl,+ result,+ ruleName,+ word: decl.value,+ });+ }+ return;+ }++ // Border radius check — flag raw length values, allow var()/calc() expressions+ if (prop === "border-radius") {+ const hasVar = /var\(/.test(decl.value);+ if (!hasVar && value !== "0") {+ stylelint.utils.report({+ message: `Avoid hardcoded "${decl.value}" for ${prop}. Use a design token: ${tokenSuggestion}`,+ node: decl,+ result,+ ruleName,+ word: decl.value,+ });+ }+ return;+ }++ // Color properties — flag raw colors+ if (containsRawColor(value)) {+ stylelint.utils.report({+ message: `Avoid hardcoded colors for ${prop}. Use a design token: ${tokenSuggestion}`,+ node: decl,+ result,+ ruleName,+ word: decl.value,+ });+ }+ });+ };+});diff --git a/stylelint.config.mjs b/stylelint.config.mjsindex f0e759cf567..0012ce6e7ec 100644--- a/stylelint.config.mjs+++ b/stylelint.config.mjs@@ -1,6 +1,31 @@+import noCoreVariables from "./stylelint-rules/no-core-variables.mjs";+import requireDesignTokens from "./stylelint-rules/require-design-tokens.mjs";+export default {extends: ["@discourse/lint-configs/stylelint"],+ plugins: [noCoreVariables, requireDesignTokens],rules: {"media-feature-range-notation": "context",},+ overrides: [+ {+ files: [+ "**/sidebar.scss",+ "**/sidebar-*.scss",+ "**/*-sidebar.scss",+ "**/*-sidebar-*.scss",+ ],+ rules: {+ "discourse/no-core-color-variables": true,+ "discourse/require-design-tokens": true,+ },+ },+ {+ files: ["themes/horizon/scss/**"],+ rules: {+ "discourse/no-core-color-variables": null,+ "discourse/require-design-tokens": null,+ },+ },+ ],};diff --git a/themes/horizon/scss/sidebar.scss b/themes/horizon/scss/sidebar.scssindex e0f9bb6a425..e3d37a1b7fa 100644--- a/themes/horizon/scss/sidebar.scss+++ b/themes/horizon/scss/sidebar.scss@@ -3,6 +3,7 @@:root {--sidebar-section-wrapper-padding: 0 var(--space-2) var(--space-4)var(--space-4);+ --d-sidebar-active-background: var(--d-selected);}body.has-full-page-chat .sidebar-wrapper,