-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathBox.svelte
More file actions
150 lines (131 loc) · 4.37 KB
/
Copy pathBox.svelte
File metadata and controls
150 lines (131 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<script>
/**
* @typedef {1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13} SpacingScale
* @typedef {SpacingScale | string} SpacingValue
* @restProps {any}
* @slot {{}}
*/
/**
* Set the background fill using a Carbon theme token.
* @type {"background" | "layer-01" | "layer-02" | "layer-03" | "field" | "inverse"}
*/
export let fill = undefined;
/**
* Set padding on all sides. Numbers `1`–`13` use the shared layout scale; strings accept any CSS length.
* @type {SpacingValue | undefined}
*/
export let padding = undefined;
/**
* Set horizontal padding. Numbers `1`–`13` use the shared layout scale; strings accept any CSS length.
* @type {SpacingValue | undefined}
*/
export let paddingX = undefined;
/**
* Set vertical padding. Numbers `1`–`13` use the shared layout scale; strings accept any CSS length.
* @type {SpacingValue | undefined}
*/
export let paddingY = undefined;
/**
* Set margin on all sides. Numbers `1`–`13` use the shared layout scale; strings accept any CSS length.
* @type {SpacingValue | undefined}
*/
export let margin = undefined;
/**
* Set horizontal margin. Numbers `1`–`13` use the shared layout scale; strings accept any CSS length.
* @type {SpacingValue | undefined}
*/
export let marginX = undefined;
/**
* Set vertical margin. Numbers `1`–`13` use the shared layout scale; strings accept any CSS length.
* @type {SpacingValue | undefined}
*/
export let marginY = undefined;
/**
* Set the border using a Carbon border token.
* @type {"subtle" | "strong" | "interactive" | "disabled"}
*/
export let border = undefined;
/**
* Set the width. Numbers are treated as pixels; strings accept any CSS length.
* @type {number | string | undefined}
*/
export let width = undefined;
/**
* Set the max width. Numbers are treated as pixels; strings accept any CSS length.
* @type {number | string | undefined}
*/
export let maxWidth = undefined;
/**
* Set the min width. Numbers are treated as pixels; strings accept any CSS length.
* @type {number | string | undefined}
*/
export let minWidth = undefined;
/** Set to `true` to span the full width of the container */
export let fullWidth = false;
/**
* Specify the tag name.
* @type {keyof HTMLElementTagNameMap}
*/
export let tag = "div";
/** @param {"p" | "px" | "py" | "m" | "mx" | "my"} kind @param {SpacingValue | undefined} value */
function spacingClass(kind, value) {
if (value == null) return undefined;
if (typeof value === "number" && value >= 1 && value <= 13) {
return `bx--box-${kind}-${value}`;
}
return undefined;
}
/** @param {SpacingValue | undefined} value */
function spacingStyle(value) {
if (value == null) return undefined;
if (typeof value === "number" && value >= 1 && value <= 13) {
return undefined;
}
if (typeof value === "string") return value;
return undefined;
}
/** @param {number | string | undefined} value */
function lengthStyle(value) {
if (value == null) return undefined;
return typeof value === "number" ? `${value}px` : value;
}
$: boxClass = [
fill && `bx--box-fill-${fill}`,
border && `bx--box-border-${border}`,
spacingClass("p", padding),
spacingClass("px", paddingX),
spacingClass("py", paddingY),
spacingClass("m", margin),
spacingClass("mx", marginX),
spacingClass("my", marginY),
fullWidth && "bx--box-full-width",
$$restProps.class,
]
.filter(Boolean)
.join(" ");
$: resolvedWidth = lengthStyle(width);
$: resolvedMaxWidth = lengthStyle(maxWidth);
$: resolvedMinWidth = lengthStyle(minWidth);
$: resolvedPadding = spacingStyle(padding);
$: resolvedPaddingX = spacingStyle(paddingX);
$: resolvedPaddingY = spacingStyle(paddingY);
$: resolvedMargin = spacingStyle(margin);
$: resolvedMarginX = spacingStyle(marginX);
$: resolvedMarginY = spacingStyle(marginY);
</script>
<svelte:element
this={tag}
{...$$restProps}
style:width={resolvedWidth}
style:max-width={resolvedMaxWidth}
style:min-width={resolvedMinWidth}
style:padding={resolvedPadding}
style:padding-inline={resolvedPaddingX}
style:padding-block={resolvedPaddingY}
style:margin={resolvedMargin}
style:margin-inline={resolvedMarginX}
style:margin-block={resolvedMarginY}
class={boxClass}
>
<slot />
</svelte:element>