Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Flex): refactor gaps #7492

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
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
31 changes: 20 additions & 11 deletions packages/vkui/src/components/Flex/Flex.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
--vkui_internal--flex_original_margin_block: 0px;

display: flex;
margin-block: calc(
-1 * var(--vkui_internal--row_gap) + var(--vkui_internal--flex_original_margin_block)
)
var(--vkui_internal--flex_original_margin_block);
margin-inline: calc(
-1 * var(--vkui_internal--column_gap) + var(--vkui_internal--flex_original_margin_inline)
)
var(--vkui_internal--flex_original_margin_inline);
}

.Flex--margin-auto {
--vkui_internal--flex_original_margin_inline: var(--vkui--size_base_padding_horizontal--regular);
--vkui_internal--flex_original_margin_block: var(--vkui--size_base_padding_vertical--regular);

margin-block: var(--vkui--size_base_padding_horizontal--regular);
margin-inline: var(--vkui--size_base_padding_vertical--regular);
}

.Flex--withGaps {
margin-block-start: calc(
-1 * var(--vkui_internal--row_gap) + var(--vkui_internal--flex_original_margin_block)
);
margin-inline-start: calc(
-1 * var(--vkui_internal--column_gap) + var(--vkui_internal--flex_original_margin_inline)
);
}

.Flex > .Flex--margin-auto {
margin-block: 0;
margin-inline: 0;
}

.Flex--direction-column {
Expand Down Expand Up @@ -83,7 +92,7 @@
}

/* stylelint-disable-next-line @project-tools/stylelint-atomic, selector-max-universal */
.Flex > * {
margin-block: var(--vkui_internal--row_gap) 0;
margin-inline: var(--vkui_internal--column_gap) 0;
.Flex--withGaps > * {
margin-block-start: var(--vkui_internal--row_gap);
margin-inline-start: var(--vkui_internal--column_gap);
}
14 changes: 13 additions & 1 deletion packages/vkui/src/components/Flex/Flex.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ import gapStyles from '../../styles/gaps.module.css';
describe(Flex, () => {
baselineComponent(Flex);

it('should have css custom variable with gaps values', () => {
it('should have css custom variable with gaps values for several child', () => {
render(
<Flex gap={[15, 20]} data-testid="flex">
<div></div>
<div></div>
</Flex>,
);
expect(screen.getByTestId('flex')).toHaveStyle('--vkui_internal--row_gap: 20px');
expect(screen.getByTestId('flex')).toHaveStyle('--vkui_internal--column_gap: 15px');
});

it('should not have css custom variable with gaps values for one child', () => {
render(
<Flex gap={[15, 20]} data-testid="flex">
<div></div>
</Flex>,
);
expect(screen.getByTestId('flex')).not.toHaveStyle('--vkui_internal--row_gap: 20px');
expect(screen.getByTestId('flex')).not.toHaveStyle('--vkui_internal--column_gap: 15px');
});

describe('check correct classNames', () => {
it.each<{ props: Partial<FlexProps>; className: string }>([
{
Expand Down Expand Up @@ -71,6 +82,7 @@ describe(Flex, () => {
render(
<Flex {...props} data-testid="flex">
<div></div>
<div></div>
</Flex>,
);
expect(screen.getByTestId('flex')).toHaveClass(className);
Expand Down
44 changes: 31 additions & 13 deletions packages/vkui/src/components/Flex/Flex.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Children } from 'react';
import { classNames } from '@vkontakte/vkjs';
import {
calculateGap,
columnGapClassNames,
type GapProp,
type GapsProp,
rowGapClassNames,
} from '../../lib/layouts';
Expand Down Expand Up @@ -83,17 +85,11 @@ export const Flex: React.FC<FlexProps> & {
direction = 'row',
style: styleProp,
reverse = false,
children,
...props
}: FlexProps) => {
const [columnGap, rowGap] = calculateGap(gap);
const style: CSSCustomProperties = {};

if (typeof rowGap === 'number') {
style['--vkui_internal--row_gap'] = `${rowGap}px`;
}
if (typeof columnGap === 'number') {
style['--vkui_internal--column_gap'] = `${columnGap}px`;
}
const withGaps = Children.count(children) > 1 && gap;
const [columnGap, rowGap] = calculateGap(withGaps ? gap : undefined);

return (
<RootComponent
Expand All @@ -104,14 +100,36 @@ export const Flex: React.FC<FlexProps> & {
reverse && styles['Flex--reverse'],
direction !== 'row' && styles['Flex--direction-column'],
margin !== 'none' && styles['Flex--margin-auto'],
typeof columnGap === 'string' && columnGapClassNames[columnGap],
typeof rowGap === 'string' && rowGapClassNames[rowGap],
align && alignClassNames[align],
justify && justifyClassNames[justify],
withGaps && styles['Flex--withGaps'],
withGaps && getGapsPresets(rowGap, columnGap),
)}
style={{ ...styleProp, ...style }}
/>
style={withGaps ? { ...getGapsByUser(rowGap, columnGap), ...styleProp } : styleProp}
>
{children}
</RootComponent>
);
};

function getGapsPresets(rowGap?: GapProp, columnGap?: GapProp) {
return classNames(
typeof rowGap === 'string' && rowGapClassNames[rowGap],
typeof columnGap === 'string' && columnGapClassNames[columnGap],
);
}

function getGapsByUser(rowGap?: GapProp, columnGap?: GapProp) {
const style: CSSCustomProperties = {};

if (typeof rowGap === 'number') {
style['--vkui_internal--row_gap'] = `${rowGap}px`;
}
if (typeof columnGap === 'number') {
style['--vkui_internal--column_gap'] = `${columnGap}px`;
}

return style;
}

Flex.Item = FlexItem;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading