Skip to content

Commit

Permalink
screens/breakpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
oofdere committed Aug 4, 2024
1 parent 62ed468 commit f1a8d4b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
3 changes: 1 addition & 2 deletions playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { composePlugins, spacing, screens } from ".";
const a = {
filename: 'test.css',
minify: true,
code: new TextEncoder().encode('.foo {size: 0.25tw; @screen lg {size: 1tw; color: red;}}'),
code: new TextEncoder().encode('.foo {color: red; @screen sm {color: green;}}'),
...composePlugins([spacing, screens]),
}

let { code, map } = transform(a);
console.log(a)

console.log(code.toString())
49 changes: 39 additions & 10 deletions src/screens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
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 screens: Plugin<CustomAtRules> = {
customAtRules: {
screen: {
Expand All @@ -12,17 +20,38 @@ export const screens: Plugin<CustomAtRules> = {
Rule: {
custom: {
screen(rule) {
console.log(rule.body.value)
console.log(rule)
if (rule.prelude.value as string in breakpoints) {

return {
type: 'media',
value: {
rules: rule.body.value,
loc: rule.loc,
query: {
mediaQueries: [
{ raw: '(min-width: 500px)' }
]
return {
type: 'media',
value: {
rules: rule.body.value,
loc: rule.loc,
query: {
mediaQueries: [
{
mediaType: 'all',
condition: {
type: 'feature',
value: {
type: 'plain',
name: "min-width",
value: {
type: 'length',
value: {
type: 'value',
value: {
unit: 'rem',
value: breakpoints[rule.prelude.value]
}
}
}
}
}
}
]
}
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/screens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, test } from "bun:test";
import { testHelper } from "./test";
import { screens, spacing } from "../index";


test("sm", () => {
expect(testHelper([screens], '.foo {color: red; @screen sm {color: green;}}'))
.toBe('.foo{color:red;@media (min-width:40rem){&{color:green}}}')
})

test("md", () => {
expect(testHelper([screens], '.foo {color: red; @screen md {color: green;}}'))
.toBe('.foo{color:red;@media (min-width:48rem){&{color:green}}}')
})

test("lg", () => {
expect(testHelper([screens], '.foo {color: red; @screen lg {color: green;}}'))
.toBe('.foo{color:red;@media (min-width:64rem){&{color:green}}}')
})

test("xl", () => {
expect(testHelper([screens], '.foo {color: red; @screen xl {color: green;}}'))
.toBe('.foo{color:red;@media (min-width:80rem){&{color:green}}}')
})

test("xxl", () => {
expect(testHelper([screens], '.foo {color: red; @screen xxl {color: green;}}'))
.toBe('.foo{color:red;@media (min-width:96rem){&{color:green}}}')
})
File renamed without changes.

0 comments on commit f1a8d4b

Please sign in to comment.