Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions lib/ui/mixins/setSpace.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
import { space, type SpaceValue } from "ui/settings";
import fluidify from "./ofMixins/fluidify";

export const setSpace = (args: string, force?: "force") => {
const prop: keyof typeof properties = args.substring(0, 1);
const pos: SpaceValue = args.substring(1, 2);
const size: SpaceValue = args.substring(2, 3);
const properties = {
b: "border-width",
m: "margin",
p: "padding",
};
const positions: Record<SpaceValue, string> = {
t: "top",
b: "bottom",
l: "left",
r: "right",
};
type PropertyValue = keyof typeof properties;
type PositionValue = "a" | "h" | "v" | keyof typeof positions;
type SpaceShorthand = `${PropertyValue}${PositionValue}${SpaceValue}`;

const properties = {
b: "border-width",
m: "margin",
p: "padding",
};
const positions = {
t: "top",
b: "bottom",
l: "left",
r: "right",
};

export const setSpace = (args: SpaceShorthand, force?: "force") => {
const prop: PropertyValue = args.substring(0, 1) as PropertyValue;
const pos: PositionValue = args.substring(1, 2) as PositionValue;
const size: SpaceValue = args.substring(2, 3) as SpaceValue;
const isImportant = force === "force";

switch (pos) {
// a = all sides
case "a":
return fluidify(
`${properties[prop]}`,
space[size][0],
space[size][1],
isImportant
);
case "k":
return fluidify(
[`${properties[prop]}-left`, `${properties[prop]}-right`],
space[size][0],
space[size][1],
isImportant
);
// h = horizontal (left & right)
case "h":
return fluidify(
[`${properties[prop]}-left`, `${properties[prop]}-right`],
space[size][0],
space[size][1],
isImportant
);
// v = vertical (top & bottom)
case "v":
return fluidify(
[`${properties[prop]}-top`, `${properties[prop]}-bottom`],
space[size][0],
space[size][1],
isImportant
);
// default: single side (top / bottom / left / right)
default:
return fluidify(
`${properties[prop]}-${positions[pos]}`,
Expand All @@ -57,4 +59,5 @@ export const setSpace = (args: string, force?: "force") => {
}
};

// Re-export for use with typing setSpace()
export { SpaceValue };
Loading