Skip to content

Commit 6b231ab

Browse files
authored
Merge pull request #14 from klmhyeonwoo/feature/Spacing
feat: Add spacing hook lib
2 parents 04ea779 + ecdb250 commit 6b231ab

File tree

10 files changed

+106
-4
lines changed

10 files changed

+106
-4
lines changed

build.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async function build(path) {
3838
dts: { only: true },
3939
outDir: dist,
4040
silent: true,
41-
external: [/@layer-lib\/.+/, /@layer-ui\/.+/, /@layer-core\/.+/],
41+
external: [/@layer-lib\/.+/, /@layer-ui\/.+/, /@layer-core\/.+/, /@layer-utils\/.+/],
4242
});
4343

4444
console.log(`Built ${path}/dist with Vite`);

demo/src/App.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { Button } from "@layer-ui/button";
2+
import { getSpacingStyle } from "@layer-utils/getSpacingStyle";
23
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@layer-ui/tabs";
34
import { tabsListStyle, tabsTriggerStyle } from "./styles/tabs.css";
45
import "./styles/app.css";
56

67
function App() {
8+
const spacingStyle = getSpacingStyle({ size: 50, direction: "vertical" });
9+
710
return (
811
<div>
912
<Button>Click me</Button>
13+
<div>
14+
<h1>Title Above</h1>
15+
<div style={spacingStyle}> This is 10px Spacing </div>
16+
<p>This content is vertically spaced.</p>
17+
</div>
1018
<Tabs defaultValue={"foo"} onValueChange={(va) => console.log(va)}>
1119
<TabsList className={tabsListStyle}>
1220
<TabsTrigger className={tabsTriggerStyle} value={"foo"}>

demo/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default defineConfig({
1010
alias: {
1111
"@layer-ui": path.resolve(__dirname, "../packages/ui"),
1212
"@layer-lib": path.resolve(__dirname, "../packages/lib"),
13+
"@layer-utils": path.resolve(__dirname, "../packages/utils"),
1314
"@layer-ui/button": path.resolve(__dirname, "../packages/ui/button/src"),
1415
"@layer-ui/tabs": path.resolve(__dirname, "../packages/ui/tabs/src"),
1516
"@layer-lib/react-context": path.resolve(__dirname, "../packages/lib/react-context/src"),

layer-ui-0.0.1.tgz

442 KB
Binary file not shown.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"test": "vitest"
1212
},
1313
"dependencies": {
14-
"@layer/lib": "file:layer-lib-0.0.1.tgz",
1514
"@vanilla-extract/css": "^1.16.0",
1615
"react": "^18.3.1",
1716
"react-dom": "^18.3.1"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@layer-utils/getSpacingStyle",
3+
"version": "0.0.1",
4+
"license": "MIT",
5+
"exports": {
6+
".": {
7+
"import": {
8+
"types": "./dist/index.d.mts",
9+
"default": "./dist/index.mjs"
10+
},
11+
"require": {
12+
"types": "./dist/index.d.ts",
13+
"default": "./dist/index.js"
14+
}
15+
}
16+
},
17+
"source": "./src/index.ts",
18+
"main": "./dist/index.js",
19+
"module": "./dist/index.mjs",
20+
"types": "./dist/index.d.ts",
21+
"files": [
22+
"dist",
23+
"README.md"
24+
],
25+
"sideEffects": false,
26+
"scripts": {
27+
"clean": "rm -rf dist",
28+
"version": "yarn version"
29+
},
30+
"peerDependencies": {
31+
"@types/react": "*",
32+
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
33+
},
34+
"peerDependenciesMeta": {
35+
"@types/react": {
36+
"optional": true
37+
}
38+
},
39+
"repository": {
40+
"type": "git",
41+
"url": "git+https://github.com/klmhyeonwoo/layer-design-system.git"
42+
},
43+
"bugs": {
44+
"url": "https://github.com/klmhyeonwoo/layer-design-system/issues"
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
type Direction = "all" | "horizontal" | "vertical" | "top" | "bottom" | "left" | "right";
2+
3+
type Unit = "px" | "rem" | "em" | "%";
4+
5+
type SpacingOptions = {
6+
size: number | "small" | "medium" | "large";
7+
direction: Direction;
8+
unit?: Unit;
9+
};
10+
11+
const sizeMap = {
12+
small: 4,
13+
medium: 8,
14+
large: 16,
15+
} as const;
16+
17+
const directionStyles = {
18+
all: ["margin"],
19+
horizontal: ["marginLeft", "marginRight"],
20+
vertical: ["marginTop", "marginBottom"],
21+
top: ["marginTop"],
22+
bottom: ["marginBottom"],
23+
left: ["marginLeft"],
24+
right: ["marginRight"],
25+
} as const;
26+
27+
/**
28+
* @description getSpacingStyle is a library that makes the user's spacing style more efficient.
29+
* @param { Object } options
30+
* @param { number | "small" | "medium" | "large" } options.size
31+
* @param { "all" | "horizontal" | "vertical" | "top" | "bottom" | "left" | "right" } options.direction
32+
* @param { "px" | "rem" | "em" | "%" } [options.unit="px"]
33+
* @returns { Record<string, string | number> }
34+
*/
35+
export const getSpacingStyle = ({ size, direction, unit = "px" }: SpacingOptions): Record<string, string | number> => {
36+
const spacingValue = typeof size === "number" ? size : sizeMap[size];
37+
const valueWithUnit = `${spacingValue}${unit}`;
38+
const styles: Record<string, string | number> = {};
39+
40+
directionStyles[direction].forEach((cur) => {
41+
styles[cur] = valueWithUnit;
42+
});
43+
44+
return styles;
45+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { getSpacingStyle } from "./getSpacingStyle";

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"compilerOptions": {
33
"baseUrl": ".",
44
"allowJs": false,
5-
65
"incremental": true,
76
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
87
"target": "ES2022",
@@ -41,6 +40,9 @@
4140
"@layer-ui/typography": ["./packages/ui/typography/src"],
4241
"@layer-ui/tabs": ["./packages/ui/tabs/src"],
4342

43+
// utils
44+
"@layer-utils/getSpacingStyle": ["./packages/utils/getSpacingStyle/src"],
45+
4446
// core
4547
"@layer-core/primitive": ["./packages/core/primitive/src"]
4648
}

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineConfig({
77
target: "es2022",
88
minify: "terser",
99
rollupOptions: {
10-
external: [/@layer-lib\/.+/, /@layer-ui\/.+/, /@layer-core\/.+/],
10+
external: [/@layer-lib\/.+/, /@layer-ui\/.+/, /@layer-core\/.+/, /@layer-utils\/.+/],
1111
treeshake: true,
1212
},
1313
},

0 commit comments

Comments
 (0)