Skip to content

Commit 5d71b81

Browse files
committed
feat(component): adds grid mixin
1 parent 7229827 commit 5d71b81

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/mixins.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ export const flex = (direction: string, align: string, justify: string) => css`
2424
align-items: ${align};
2525
justify-content: ${justify};
2626
`;
27+
28+
export const grid = (cols: number, colGap: number, rowGap?: number) =>
29+
css`
30+
display: grid;
31+
grid-template-columns: repeat(${cols}, 1fr);
32+
grid-column-gap: ${rem(colGap)};
33+
grid-row-gap: ${rem(typeof rowGap === "number" ? rowGap : colGap)};
34+
`;

src/styled-tidy.test.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
rem,
2222
opacify,
2323
transparentize,
24-
flex
24+
flex,
25+
grid
2526
} from "./styled-tidy";
2627

2728
declare interface TestProps {
@@ -309,4 +310,28 @@ describe("styed-tidy", () => {
309310
expect(test).toHaveStyleRule("justify-content", "flex-end");
310311
});
311312
});
313+
314+
describe("'grid' mixin", () => {
315+
it("sets the given grid CSS attributes", () => {
316+
const Test = styled.div<TestProps>`
317+
${grid(4, 16, 24)};
318+
`;
319+
const { getByText } = setup(<Test>test</Test>);
320+
const test = getByText("test");
321+
322+
expect(test).toHaveStyleRule("display", "grid");
323+
expect(test).toHaveStyleRule("grid-template-columns", "repeat(4,1fr)");
324+
expect(test).toHaveStyleRule("grid-column-gap", "1rem");
325+
expect(test).toHaveStyleRule("grid-row-gap", "1.5rem");
326+
});
327+
328+
it("sets the row gap equal to the column gap when row gap is not given", () => {
329+
const Test = styled.div<TestProps>`
330+
${grid(2, 16)};
331+
`;
332+
const { getByText } = setup(<Test>test</Test>);
333+
334+
expect(getByText("test")).toHaveStyleRule("grid-row-gap", "1rem");
335+
});
336+
});
312337
});

src/styled-tidy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export {
1010
under
1111
} from "./matchers";
1212
export { minWidth, maxWidth, minHeight, maxHeight } from "./media";
13-
export { rem, opacify, transparentize, flex } from "./mixins";
13+
export { rem, opacify, transparentize, flex, grid } from "./mixins";

0 commit comments

Comments
 (0)