Skip to content

Commit

Permalink
refactor(themes): use .dark selector instead of html.dark (#182)
Browse files Browse the repository at this point in the history
## Description

Use `.dark` selector for the dark theme instead of `html.dark`.
Similarly we could use e.g. `.onyx-lidl`, `.onyx-kaufland` for our
future themes. This will give us more flexibility because it allows us
to use a specific theme only for a single component (e.g. by setting
`class="onyx-lidl"` on a `<div>`.
  • Loading branch information
larsrickert authored Jan 23, 2024
1 parent 113d661 commit d1fe8e3
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 11 deletions.
23 changes: 23 additions & 0 deletions .changeset/small-rats-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"@sit-onyx/figma-utils": major
---

feat: support {mode} placeholder for CSS selector

Previously the mode name was automatically appended to the selector if the selector was
something other than `:root`. This is no longer the case.
Instead use the more explicit/flexible placeholder `{mode}` for this.

**Old**

```sh
npx @sit-onyx/figma-utils import-variables -k "your-file-key" -t "your-token" -m dark -s html
# resulted in selector "html.dark"
```

**New**

```sh
npx @sit-onyx/figma-utils import-variables -k "your-file-key" -t "your-token" -m dark -s html.{mode}
# results in selector "html.dark"
```
2 changes: 1 addition & 1 deletion .github/workflows/import-figma.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:

- name: Import dark variables
run: |
pnpm run @sit-onyx/figma-utils import-variables -k "${{ vars.FIGMA_FILE_KEY }}" -t "${{ secrets.FIGMA_TOKEN }}" -d "../sit-onyx/src/styles" -m dark -s html
pnpm run @sit-onyx/figma-utils import-variables -k "${{ vars.FIGMA_FILE_KEY }}" -t "${{ secrets.FIGMA_TOKEN }}" -d "../sit-onyx/src/styles" -m dark -s .{mode}
working-directory: packages/figma-utils

- name: Create pull request
Expand Down
2 changes: 1 addition & 1 deletion packages/figma-utils/src/commands/import-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const importCommand = new Command("import-variables")
)
.option(
"-s, --selector <string>",
'CSS selector to use for the CSS format. The mode name will be added to the selector if it is set to something other than ":root", e.g. for the mode named "dark", passing the selector "html" will result in "html.dark"',
'CSS selector to use for the CSS format. You can use {mode} as placeholder for the mode name, so e.g. for the mode named "dark", passing the selector "html.{mode}" will result in "html.dark"',
":root",
)
.action(importCommandAction);
Expand Down
6 changes: 3 additions & 3 deletions packages/figma-utils/src/utils/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ describe("generate.ts", () => {
`);
});

test("should generate as CSS with custom selector", () => {
const fileContent = generateAsCSS(mockData, { selector: "html" });
test("should generate as CSS with custom selector and replace mode placeholder", () => {
const fileContent = generateAsCSS(mockData, { selector: "html.{mode}, .selector-{mode}" });

expect(fileContent).toBe(`/**
* Do not edit directly.
* This file contains the specific variables for the "test-mode-1" theme.
* Imported from Figma API on Sun, 07 Jan 2024 13:42:00 GMT
*/
html.test-mode-1 {
html.test-mode-1, .selector-test-mode-1 {
--test-1: #ffffff;
--test-2: 1rem;
--test-3: var(--test-2);
Expand Down
9 changes: 4 additions & 5 deletions packages/figma-utils/src/utils/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ export type BaseGenerateOptions = {

export type GenerateAsCSSOptions = BaseGenerateOptions & {
/**
* Selector to use for the CSS format. The mode name will be added to the selector
* if it is set to something other than ":root"
* Selector to use for the CSS format. You can use {mode} as placeholder for the mode name.
*
* @default ":root"
* @example
* for the mode named "dark", passing the selector "html" will result in "html.dark"
* for the mode named "dark", passing the selector "html.{mode}" will result in "html.dark"
*/
selector?: string;
};
Expand All @@ -37,8 +36,8 @@ export const generateAsCSS = (data: ParsedVariable, options?: GenerateAsCSSOptio
options,
);

let fullSelector = options?.selector?.trim() || ":root";
if (fullSelector !== ":root") fullSelector += `.${data.modeName}`;
const fullSelector =
options?.selector?.trim().replaceAll("{mode}", data.modeName ?? "") || ":root";

return `${generateTimestampComment(data.modeName)}
${fullSelector} {\n${variableContent.join("\n")}\n}\n`;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/sit-onyx/src/components/TestInput/TestInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ watch(
width: max-content;
display: inline-block;
font-family: var(--onyx-font-family);
color: var(--onyx-color-text-neutral-intense);
&__label {
margin-right: 8px;
Expand Down
2 changes: 1 addition & 1 deletion packages/sit-onyx/src/styles/variables-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This file contains the specific variables for the "dark" theme.
* Imported from Figma API on Fri, 19 Jan 2024 09:37:12 GMT
*/
html.dark {
.dark {
--onyx-color-base-action-100: var(--onyx-color-themed-action-1100);
--onyx-color-base-action-200: var(--onyx-color-themed-action-1000);
--onyx-color-base-action-300: var(--onyx-color-themed-action-900);
Expand Down

0 comments on commit d1fe8e3

Please sign in to comment.