Skip to content

Commit 36c9b2b

Browse files
committed
feat(component): adds position mixin
1 parent 5d71b81 commit 36c9b2b

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/mixins.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,17 @@ export const grid = (cols: number, colGap: number, rowGap?: number) =>
3232
grid-column-gap: ${rem(colGap)};
3333
grid-row-gap: ${rem(typeof rowGap === "number" ? rowGap : colGap)};
3434
`;
35+
36+
export const position = (
37+
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+
`;

src/styled-tidy.test.tsx

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
opacify,
2323
transparentize,
2424
flex,
25-
grid
25+
grid,
26+
position
2627
} from "./styled-tidy";
2728

2829
declare interface TestProps {
@@ -334,4 +335,62 @@ describe("styed-tidy", () => {
334335
expect(getByText("test")).toHaveStyleRule("grid-row-gap", "1rem");
335336
});
336337
});
338+
339+
describe("'position' mixin", () => {
340+
it("sets the given position CSS attributes", () => {
341+
const Test = styled.div<TestProps>`
342+
${position("fixed", "1rem", "2rem", "3rem", "4rem")};
343+
`;
344+
const { getByText } = setup(<Test>test</Test>);
345+
const test = getByText("test");
346+
347+
expect(test).toHaveStyleRule("position", "fixed");
348+
expect(test).toHaveStyleRule("top", "1rem");
349+
expect(test).toHaveStyleRule("right", "2rem");
350+
expect(test).toHaveStyleRule("bottom", "3rem");
351+
expect(test).toHaveStyleRule("left", "4rem");
352+
});
353+
354+
it("sets the given position CSS attributes with single param shorthand", () => {
355+
const Test = styled.div<TestProps>`
356+
${position("absolute", "1rem")};
357+
`;
358+
const { getByText } = setup(<Test>test</Test>);
359+
const test = getByText("test");
360+
361+
expect(test).toHaveStyleRule("position", "absolute");
362+
expect(test).toHaveStyleRule("top", "1rem");
363+
expect(test).toHaveStyleRule("right", "1rem");
364+
expect(test).toHaveStyleRule("bottom", "1rem");
365+
expect(test).toHaveStyleRule("left", "1rem");
366+
});
367+
368+
it("sets the given position CSS attributes with double param shorthand", () => {
369+
const Test = styled.div<TestProps>`
370+
${position("absolute", "1rem", "2rem")};
371+
`;
372+
const { getByText } = setup(<Test>test</Test>);
373+
const test = getByText("test");
374+
375+
expect(test).toHaveStyleRule("position", "absolute");
376+
expect(test).toHaveStyleRule("top", "1rem");
377+
expect(test).toHaveStyleRule("right", "2rem");
378+
expect(test).toHaveStyleRule("bottom", "1rem");
379+
expect(test).toHaveStyleRule("left", "2rem");
380+
});
381+
382+
it("sets the given position CSS attributes with triple param shorthand", () => {
383+
const Test = styled.div<TestProps>`
384+
${position("absolute", "1rem", "2rem", "3rem")};
385+
`;
386+
const { getByText } = setup(<Test>test</Test>);
387+
const test = getByText("test");
388+
389+
expect(test).toHaveStyleRule("position", "absolute");
390+
expect(test).toHaveStyleRule("top", "1rem");
391+
expect(test).toHaveStyleRule("right", "2rem");
392+
expect(test).toHaveStyleRule("bottom", "3rem");
393+
expect(test).toHaveStyleRule("left", "2rem");
394+
});
395+
});
337396
});

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, grid } from "./mixins";
13+
export { rem, opacify, transparentize, flex, grid, position } from "./mixins";

0 commit comments

Comments
 (0)