Skip to content

Commit c2025cb

Browse files
committed
feat(component): allows stirngs or numbers for grid and position mixins
1 parent 0ebbdd7 commit c2025cb

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

src/mixins.tsx

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { css } from "styled-components";
44
import { getColorChannels } from "./helpers";
55

66
export const rem = (pixels: number, base: number = 16): string =>
7-
`${pixels / base}rem`;
7+
pixels === 0 ? "0" : `${pixels / base}rem`;
88

99
export const opacify = (color: string, adjust: number): string => {
1010
const [r, g, b, a] = getColorChannels(color);
@@ -25,24 +25,41 @@ export const flex = (direction: string, align: string, justify: string) => css`
2525
justify-content: ${justify};
2626
`;
2727

28-
export const grid = (cols: number, colGap: number, rowGap?: number) =>
29-
css`
28+
export const grid = (
29+
cols: number,
30+
colGap: string | number,
31+
rowGap?: string | number
32+
) => {
33+
const colGapUnits = typeof colGap === "number" ? rem(colGap) : colGap;
34+
const rowGapUnits =
35+
typeof rowGap === "number" ? rem(rowGap) : rowGap || colGapUnits;
36+
37+
return css`
3038
display: grid;
3139
grid-template-columns: repeat(${cols}, 1fr);
32-
grid-column-gap: ${rem(colGap)};
33-
grid-row-gap: ${rem(typeof rowGap === "number" ? rowGap : colGap)};
40+
grid-column-gap: ${colGapUnits};
41+
grid-row-gap: ${rowGapUnits};
3442
`;
43+
};
3544

3645
export const position = (
3746
value: string,
38-
top: string,
39-
right?: string,
40-
bottom?: string,
41-
left?: string
42-
) => css`
43-
position: ${value};
44-
top: ${top};
45-
right: ${right || top};
46-
bottom: ${bottom || top};
47-
left: ${left || right || top};
48-
`;
47+
top: string | number,
48+
right?: string | number,
49+
bottom?: string | number,
50+
left?: string | number
51+
) => {
52+
const topUnits = typeof top === "number" ? rem(top) : top;
53+
const rightUnits = typeof right === "number" ? rem(right) : right || topUnits;
54+
const bottomUnits =
55+
typeof bottom === "number" ? rem(bottom) : bottom || topUnits;
56+
const leftUnits = typeof left === "number" ? rem(left) : left || rightUnits;
57+
58+
return css`
59+
position: ${value};
60+
top: ${topUnits};
61+
right: ${rightUnits};
62+
bottom: ${bottomUnits};
63+
left: ${leftUnits};
64+
`;
65+
};

src/styled-tidy.test.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ describe("styed-tidy", () => {
311311
expect(rem(4)).toBe("0.25rem");
312312
expect(rem(16, 8)).toBe("2rem");
313313
expect(rem(16, 32)).toBe("0.5rem");
314+
expect(rem(0)).toBe("0");
314315
});
315316
});
316317

@@ -364,7 +365,7 @@ describe("styed-tidy", () => {
364365
});
365366

366367
describe("'grid' mixin", () => {
367-
it("sets the given grid CSS attributes", () => {
368+
it("sets the given grid CSS attributes from numbers", () => {
368369
const Test = styled.div<TestProps>`
369370
${grid(4, 16, 24)};
370371
`;
@@ -377,6 +378,17 @@ describe("styed-tidy", () => {
377378
expect(test).toHaveStyleRule("grid-row-gap", "1.5rem");
378379
});
379380

381+
it("sets the given grid CSS attributes from strings", () => {
382+
const Test = styled.div<TestProps>`
383+
${grid(4, "2rem", "3rem")};
384+
`;
385+
const { getByText } = setup(<Test>test</Test>);
386+
const test = getByText("test");
387+
388+
expect(test).toHaveStyleRule("grid-column-gap", "2rem");
389+
expect(test).toHaveStyleRule("grid-row-gap", "3rem");
390+
});
391+
380392
it("sets the row gap equal to the column gap when row gap is not given", () => {
381393
const Test = styled.div<TestProps>`
382394
${grid(2, 16)};
@@ -389,6 +401,20 @@ describe("styed-tidy", () => {
389401

390402
describe("'position' mixin", () => {
391403
it("sets the given position CSS attributes", () => {
404+
const Test = styled.div<TestProps>`
405+
${position("fixed", 0, 16, 32, 48)};
406+
`;
407+
const { getByText } = setup(<Test>test</Test>);
408+
const test = getByText("test");
409+
410+
expect(test).toHaveStyleRule("position", "fixed");
411+
expect(test).toHaveStyleRule("top", "0");
412+
expect(test).toHaveStyleRule("right", "1rem");
413+
expect(test).toHaveStyleRule("bottom", "2rem");
414+
expect(test).toHaveStyleRule("left", "3rem");
415+
});
416+
417+
it("sets the given position CSS attributes from strings", () => {
392418
const Test = styled.div<TestProps>`
393419
${position("fixed", "1rem", "2rem", "3rem", "4rem")};
394420
`;

0 commit comments

Comments
 (0)