Skip to content

Commit

Permalink
@light and @dark blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
oofdere committed Aug 4, 2024
1 parent f1a8d4b commit 546f9e8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 7 deletions.
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { composePlugins, type Plugin } from './src/plugin';
import { screens } from './src/screens';
import { spacing } from './src/spacing';
import { colorScheme } from './src/colorScheme';

export const crosswind = composePlugins([screens, spacing])
export const crosswind = composePlugins([screens, spacing, colorScheme])

export { composePlugins, type Plugin, screens, spacing }
export { composePlugins, type Plugin, screens, spacing, colorScheme }
6 changes: 3 additions & 3 deletions playground.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { transform } from "lightningcss";
import { composePlugins, spacing, screens } from ".";
import { composePlugins, spacing, screens, colorScheme } from ".";

const a = {
filename: 'test.css',
minify: true,
code: new TextEncoder().encode('.foo {color: red; @screen sm {color: green;}}'),
...composePlugins([spacing, screens]),
code: new TextEncoder().encode('.foo {color: red; @light {color: green;} @dark {color: yellow;}}'),
...composePlugins([colorScheme]),
}

let { code, map } = transform(a);
Expand Down
83 changes: 83 additions & 0 deletions src/colorScheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { CustomAtRules } from "lightningcss";
import type { Plugin } from "./plugin";

const breakpoints = {
sm: 40,
md: 48,
lg: 64,
xl: 80,
xxl: 96
} as const;

export const colorScheme: Plugin<CustomAtRules> = {
customAtRules: {
light: {
prelude: null,
body: 'style-block'
},
dark: {
prelude: null,
body: 'style-block'
}
},
visitor: {
Rule: {
custom: {
light(rule) {
return {
type: 'media',
value: {
rules: rule.body.value,
loc: rule.loc,
query: {
mediaQueries: [
{
mediaType: 'all',
condition: {
type: 'feature',
value: {
type: 'plain',
name: "prefers-color-scheme",
value: {
type: 'ident',
value: 'light'
}
}
}
}
]
}
}
}
},
dark(rule) {
return {
type: 'media',
value: {
rules: rule.body.value,
loc: rule.loc,
query: {
mediaQueries: [
{
mediaType: 'all',
condition: {
type: 'feature',
value: {
type: 'plain',
name: "prefers-color-scheme",
value: {
type: 'ident',
value: 'dark'
}
}
}
}
]
}
}
}
}
}
}
}
} as const;
3 changes: 1 addition & 2 deletions src/screens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export const screens: Plugin<CustomAtRules> = {
Rule: {
custom: {
screen(rule) {
console.log(rule)
if (rule.prelude.value as string in breakpoints) {
if (typeof rule.prelude.value === 'string' && rule.prelude.value in breakpoints) {

return {
type: 'media',
Expand Down
14 changes: 14 additions & 0 deletions tests/colorScheme.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from "bun:test";
import { testHelper } from "./test";
import { colorScheme } from "../index";


test("light", () => {
expect(testHelper([colorScheme], '.foo {color: red; @light {color: green;}}'))
.toBe('.foo{color:red;@media (prefers-color-scheme:light){&{color:green}}}')
})

test("dark", () => {
expect(testHelper([colorScheme], '.foo {color: red; @dark {color: green;}}'))
.toBe('.foo{color:red;@media (prefers-color-scheme:dark){&{color:green}}}')
})

0 comments on commit 546f9e8

Please sign in to comment.