Skip to content

Commit 13d724c

Browse files
committed
Syntax as numbers
1 parent d9df034 commit 13d724c

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

test/ui/grid/index.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ test("applies columns as object with string as breakpoints and default key", asy
9898
assert(html.includes("grid-template-columns:1fr"))
9999
})
100100

101+
test("applies columns as object with strings and default key", async () => {
102+
const { template } = await compile(__dirname)
103+
const html = template({
104+
columns: {
105+
default: 4,
106+
"1023px": 2,
107+
"767px": 1,
108+
},
109+
})
110+
assert(html.includes("grid-template-columns:repeat(4,1fr)"))
111+
assert(html.includes("@media (max-width:1023px)"))
112+
assert(html.includes("grid-template-columns:repeat(2,1fr)"))
113+
assert(html.includes("@media (max-width:767px)"))
114+
assert(html.includes("grid-template-columns:repeat(1,1fr)"))
115+
})
116+
117+
test("applies columns as object with string as breakpoints and default key", async () => {
118+
const { template } = await compile(__dirname)
119+
const html = template({
120+
columns: {
121+
default: 4,
122+
lg: 2,
123+
md: 1,
124+
},
125+
})
126+
assert(html.includes("grid-template-columns:repeat(4,1fr)"))
127+
assert(html.includes("@media (max-width:1023px)"))
128+
assert(html.includes("grid-template-columns:repeat(2,1fr)"))
129+
assert(html.includes("@media (max-width:767px)"))
130+
assert(html.includes("grid-template-columns:repeat(1,1fr)"))
131+
})
132+
101133
test("applies gap", async () => {
102134
const { template } = await compile(__dirname)
103135
const html = template({ gap: "16px" })

ui/grid/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ function Grid({ className, columns = 3, gap, breakpoint, style }, children) {
2424
}),
2525
...(typeof columns === "object" &&
2626
Object.keys(columns).reduce((object, key) => {
27+
const value =
28+
typeof columns[key] === "number"
29+
? `repeat(${columns[key]}, 1fr)`
30+
: columns[key]
2731
if (key === "default") {
28-
object["grid-template-columns"] = columns[key]
32+
object["grid-template-columns"] = value
2933
} else if (typeof key === "string") {
30-
const value =
34+
const maxWidth =
3135
BREAKPOINTS[key] || (key.endsWith("px") ? key : `${key}px`)
32-
object[`@media (max-width: ${value})`] = {
33-
"grid-template-columns": columns[key],
36+
37+
object[`@media (max-width: ${maxWidth})`] = {
38+
"grid-template-columns": value,
3439
}
3540
}
3641
return object

0 commit comments

Comments
 (0)