Skip to content

Commit c42ba37

Browse files
committed
feat(Stack): new component for stacked elements
1 parent 7874171 commit c42ba37

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/components/Stack/Stack.scss

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
.stack {
2+
--ydb-stack-base-z-index: 100;
3+
--ydb-stack-offset-x: 4px;
4+
--ydb-stack-offset-y: 4px;
5+
--ydb-stack-offset-x-hover: 4px;
6+
--ydb-stack-offset-y-hover: 8px;
7+
8+
position: relative;
9+
10+
&__layer {
11+
transition: transform 0.1s ease-out;
12+
13+
&:first-child {
14+
position: relative;
15+
z-index: var(--ydb-stack-base-z-index);
16+
}
17+
18+
& + & {
19+
position: absolute;
20+
z-index: calc(var(--ydb-stack-base-z-index) - var(--ydb-stack-level));
21+
top: 0;
22+
left: 0;
23+
24+
width: 100%;
25+
height: 100%;
26+
27+
transform: translate(
28+
calc(var(--ydb-stack-level) * var(--ydb-stack-offset-x)),
29+
calc(var(--ydb-stack-level) * var(--ydb-stack-offset-y))
30+
);
31+
}
32+
}
33+
34+
&:hover {
35+
.stack__layer:first-child {
36+
transform: translate(
37+
calc(-1 * var(--ydb-stack-offset-x-hover)),
38+
calc(-1 * var(--ydb-stack-offset-y-hover))
39+
);
40+
}
41+
42+
.stack__layer + .stack__layer {
43+
transform: translate(
44+
calc(
45+
var(--ydb-stack-level) * (var(--ydb-stack-offset-x-hover) * 2) -
46+
var(--ydb-stack-offset-x-hover)
47+
),
48+
calc(
49+
var(--ydb-stack-level) * (var(--ydb-stack-offset-y-hover) * 2) -
50+
var(--ydb-stack-offset-y-hover)
51+
)
52+
);
53+
}
54+
}
55+
}

src/components/Stack/Stack.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
import cn from 'bem-cn-lite';
3+
4+
import './Stack.scss';
5+
6+
interface StackProps {
7+
className?: string;
8+
}
9+
10+
const LAYER_CSS_VAR = '--ydb-stack-level';
11+
12+
const b = cn('stack');
13+
14+
export const Stack: React.FC<StackProps> = ({children, className}) => (
15+
<div className={b(null, className)}>
16+
{
17+
React.Children.map(children, (child, index) => {
18+
if (!React.isValidElement(child)) {
19+
return null;
20+
}
21+
22+
return (
23+
<div
24+
className={b('layer')}
25+
style={{
26+
[LAYER_CSS_VAR]: index,
27+
} as React.CSSProperties}
28+
>
29+
{child}
30+
</div>
31+
);
32+
})
33+
}
34+
</div>
35+
);

0 commit comments

Comments
 (0)