Skip to content

Commit 13e13ed

Browse files
authored
feat(PinField): wrap to next line when digits don't fit in viewport (#1359)
https://jira.tid.es/browse/WEB-2146
1 parent da0a953 commit 13e13ed

7 files changed

Lines changed: 83 additions & 19 deletions
60.1 KB
Loading
1.86 KB
Loading

src/__screenshot_tests__/inline-screenshot-test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ test('Inline wrap', async () => {
3232
expect(image).toMatchImageSnapshot();
3333
});
3434

35+
test('Inline wrap vertical space', async () => {
36+
const page = await openStoryPage({id: 'layout-inline--wrap', args: {verticalSpace: 16}});
37+
38+
const image = await page.screenshot();
39+
expect(image).toMatchImageSnapshot();
40+
});
41+
3542
// This test is unstable (https://jira.tid.es/browse/WEB-1648)
3643
// eslint-disable-next-line jest/no-disabled-tests
3744
test.skip('Inline negative space', async () => {

src/__stories__/inline-story.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,14 @@ Default.storyName = 'Inline';
107107

108108
type Args = {
109109
space: React.ComponentProps<typeof Inline>['space'];
110+
verticalSpace: React.ComponentProps<typeof Inline>['verticalSpace'];
110111
numItems: number;
111112
};
112113

113-
export const Wrap: StoryComponent<Args> = ({space, numItems}) => {
114+
export const Wrap: StoryComponent<Args> = ({space, verticalSpace, numItems}) => {
114115
const tagTypes = ['active', 'inactive', 'success', 'warning', 'error', 'promo', 'info'] as const;
115116
return (
116-
<Inline space={space} wrap>
117+
<Inline space={space} verticalSpace={verticalSpace} wrap>
117118
{Array.from({length: numItems}, (_, i) => {
118119
const type = tagTypes[i % tagTypes.length];
119120
return (
@@ -128,6 +129,7 @@ export const Wrap: StoryComponent<Args> = ({space, numItems}) => {
128129

129130
Wrap.args = {
130131
space: 8,
132+
verticalSpace: 8,
131133
numItems: 50,
132134
};
133135

@@ -136,6 +138,10 @@ Wrap.argTypes = {
136138
options: [0, 2, 4, 8, 12, 16, 24, 32, 40, 48, 56, 64],
137139
control: {type: 'select'},
138140
},
141+
verticalSpace: {
142+
options: [0, 2, 4, 8, 12, 16, 24, 32, 40, 48, 56, 64],
143+
control: {type: 'select'},
144+
},
139145
};
140146

141147
Wrap.storyName = 'Inline wrap';

src/inline.css.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,42 @@ const spaceMobile = createVar();
66
const spaceTablet = createVar();
77
const spaceDesktop = createVar();
88

9-
export const vars = {space, spaceMobile, spaceTablet, spaceDesktop};
9+
const verticalSpace = createVar();
10+
const verticalSpaceMobile = createVar();
11+
const verticalSpaceTablet = createVar();
12+
const verticalSpaceDesktop = createVar();
13+
14+
export const vars = {
15+
space,
16+
spaceMobile,
17+
spaceTablet,
18+
spaceDesktop,
19+
verticalSpace,
20+
verticalSpaceMobile,
21+
verticalSpaceTablet,
22+
verticalSpaceDesktop,
23+
};
1024

1125
export const marginInline = style({
12-
marginTop: `calc(${space} * -1)`,
26+
marginTop: `calc(${verticalSpace} * -1)`,
1327
marginLeft: `calc(${space} * -1)`,
1428
'@media': {
1529
[mq.mobile]: {
1630
vars: {
1731
[space]: spaceMobile,
32+
[verticalSpace]: verticalSpaceMobile,
1833
},
1934
},
2035
[mq.tablet]: {
2136
vars: {
2237
[space]: fallbackVar(spaceTablet, spaceMobile),
38+
[verticalSpace]: fallbackVar(verticalSpaceTablet, verticalSpaceMobile),
2339
},
2440
},
2541
[mq.desktopOrBigger]: {
2642
vars: {
2743
[space]: spaceDesktop,
44+
[verticalSpace]: verticalSpaceDesktop,
2845
},
2946
},
3047
},
@@ -39,14 +56,15 @@ const supportsFlexGap = '(display: flex) and (gap: 0px)';
3956
export const inline = style({
4057
pointerEvents: 'none', // to prevent negative margins from affecting clickable areas
4158
gridAutoFlow: 'column',
42-
marginTop: `calc(${space} * -1)`,
59+
marginTop: `calc(${fallbackVar(verticalSpace, space)} * -1)`,
4360
marginLeft: `calc(${space} * -1)`,
4461
'@supports': {
4562
[supportsFlexGap]: {
4663
margin: 0, // restore
4764
pointerEvents: 'auto', // restore
4865
flexDirection: 'row',
4966
gap: space,
67+
rowGap: fallbackVar(verticalSpace, space),
5068
},
5169
},
5270
});
@@ -82,7 +100,7 @@ export const stringSpaceWithWrap = style({
82100

83101
globalStyle(`${marginInline} > div`, {
84102
marginLeft: space,
85-
marginTop: space,
103+
marginTop: fallbackVar(verticalSpace, space),
86104
});
87105

88106
globalStyle(`${inline} > div`, {

src/inline.tsx

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,56 @@ const calcSpaceValue = (space: NumericSpace | FlexSpace) => {
2020
}
2121
};
2222

23-
const calcInlineVars = (space: FlexSpace | ByBreakpoint<NumericSpace>) => {
24-
if (typeof space === 'number' || typeof space === 'string') {
25-
return {
26-
[styles.vars.space]: calcSpaceValue(space),
23+
const calcInlineVars = (
24+
space: FlexSpace | ByBreakpoint<NumericSpace>,
25+
verticalSpace?: ByBreakpoint<NumericSpace>
26+
) => {
27+
const calcSpaceVars = (
28+
space: FlexSpace | ByBreakpoint<NumericSpace>,
29+
varNames: {default: string; mobile: string; tablet: string; desktop: string}
30+
) => {
31+
if (typeof space === 'number' || typeof space === 'string') {
32+
return {
33+
[varNames.default]: calcSpaceValue(space),
34+
};
35+
}
36+
const vars = {
37+
[varNames.mobile]: calcSpaceValue(space.mobile),
38+
[varNames.desktop]: calcSpaceValue(space.desktop),
2739
};
28-
}
29-
const vars = {
30-
[styles.vars.spaceMobile]: calcSpaceValue(space.mobile),
31-
[styles.vars.spaceDesktop]: calcSpaceValue(space.desktop),
40+
if (space.tablet) {
41+
vars[varNames.tablet] = calcSpaceValue(space.tablet);
42+
}
43+
return vars;
3244
};
33-
if (space.tablet) {
34-
vars[styles.vars.spaceTablet] = calcSpaceValue(space.tablet);
45+
46+
const spaceVars = calcSpaceVars(space, {
47+
default: styles.vars.space,
48+
mobile: styles.vars.spaceMobile,
49+
tablet: styles.vars.spaceTablet,
50+
desktop: styles.vars.spaceDesktop,
51+
});
52+
53+
if (verticalSpace) {
54+
const verticalSpaceVars = calcSpaceVars(verticalSpace, {
55+
default: styles.vars.verticalSpace,
56+
mobile: styles.vars.verticalSpaceMobile,
57+
tablet: styles.vars.verticalSpaceTablet,
58+
desktop: styles.vars.verticalSpaceDesktop,
59+
});
60+
61+
return {
62+
...spaceVars,
63+
...verticalSpaceVars,
64+
};
3565
}
36-
return vars;
66+
67+
return spaceVars;
3768
};
3869

3970
type Props = {
4071
space: FlexSpace | ByBreakpoint<NumericSpace>;
72+
verticalSpace?: ByBreakpoint<NumericSpace>;
4173
alignItems?: 'flex-start' | 'flex-end' | 'center' | 'stretch' | 'baseline';
4274
children: React.ReactNode;
4375
className?: string;
@@ -51,6 +83,7 @@ type Props = {
5183

5284
const Inline = ({
5385
space,
86+
verticalSpace,
5487
className,
5588
children,
5689
role,
@@ -72,7 +105,7 @@ const Inline = ({
72105
wrap ? styles.wrap : fullWidth ? styles.fullWidth : styles.noFullWidth,
73106
isStringSpace ? (wrap ? styles.stringSpaceWithWrap : styles.stringSpace) : styles.marginInline
74107
)}
75-
style={{...applyCssVars(calcInlineVars(space)), alignItems}}
108+
style={{...applyCssVars(calcInlineVars(space, verticalSpace)), alignItems}}
76109
role={role}
77110
aria-label={ariaLabel}
78111
aria-labelledby={ariaLabel ? undefined : ariaLabelledBy}

src/pin-field.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const PinInput = ({
152152
};
153153

154154
return (
155-
<Inline space={8}>
155+
<Inline space={8} wrap verticalSpace={16}>
156156
{Array.from({length}).map((_, index) => (
157157
<div
158158
key={index}

0 commit comments

Comments
 (0)