Skip to content

Commit

Permalink
feat(Spacing): remove 3xs size type (#7634)
Browse files Browse the repository at this point in the history
h2. Описание

Сейчас в некоторых местах в css используется токен `--vkui--spacing_size_3xs`, но этот токен устарел и равен токену `--vkui--spacing_size_2xs`. Нужно избавиться от использования этого токена в css

h2. Изменения

- Выпилил использование этого токена в компоненте Spacing, соответственно удалил один из вариантов пропа size
- Реализовал кодмод, чтобы size: 3xs преобразовывался в 2xs в Spacing.
- Заменил использование токена `--vkui--spacing_size_3xs` на `--vkui--spacing_size_2xs` в css компонента Header

h2. Release notes

h2. BREAKING CHANGE
- Spacing: удален вариант значения пропа size `3xs` вместо него можно использовать `2xs`, он такой же по значению
  ```diff
  - <Spacing size="3xs" />
  + <Spacing size="2xs" />
  ```
  • Loading branch information
EldarMuhamethanov authored Sep 20, 2024
1 parent e98a9e4 commit 491772f
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Separator, Spacing } from '@vkontakte/vkui';
import React from 'react';

const App = () => {
return (
<React.Fragment>
<Spacing size="3xs">
<Separator/>
</Spacing>

<Spacing size="3xs" />

<Spacing size={"3xs"} />
</React.Fragment>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`spacing transforms correctly 1`] = `
"import { Separator, Spacing } from '@vkontakte/vkui';
import React from 'react';
const App = () => {
return (
(<React.Fragment>
<Spacing size="2xs">
<Separator/>
</Spacing>
<Spacing size="2xs" />
<Spacing size={"2xs"} />
</React.Fragment>)
);
};"
`;
11 changes: 11 additions & 0 deletions packages/codemods/src/transforms/v7/__tests__/spacing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
jest.autoMockOff();
import { defineSnapshotTestFromFixture } from '../../../testHelpers/testHelper';

const name = 'spacing';
const fixtures = ['basic'] as const;

describe(name, () => {
fixtures.forEach((test) =>
defineSnapshotTestFromFixture(__dirname, name, global.TRANSFORM_OPTIONS, `${name}/${test}`),
);
});
22 changes: 20 additions & 2 deletions packages/codemods/src/transforms/v7/common/remapSizePropValue.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Collection, JSCodeshift } from 'jscodeshift';
import { API, Collection, JSCodeshift } from 'jscodeshift';
import { report } from '../../../report';

export const remapSizePropValue = ({
j,
source,
sizesMap,
componentName,
api,
}: {
api: API;
j: JSCodeshift;
source: Collection;
sizesMap: Record<string, string>;
Expand All @@ -22,8 +25,23 @@ export const remapSizePropValue = ({
return attributeName === 'size';
})
.forEach((attribute) => {
let nodeToReplaceValue;
if (attribute.node.value?.type === 'JSXExpressionContainer') {
const expression = attribute.node.value.expression;
if (expression.type === 'StringLiteral') {
nodeToReplaceValue = expression;
}
}
if (attribute.node.value?.type === 'StringLiteral') {
attribute.node.value.value = sizesMap[attribute.node.value.value];
nodeToReplaceValue = attribute.node.value;
}
if (nodeToReplaceValue) {
const newValue = sizesMap[nodeToReplaceValue.value];
if (newValue) {
nodeToReplaceValue.value = newValue;
}
} else {
report(api, `Manual changes required for ${componentName}'s "size" prop.`);
}
});
};
1 change: 1 addition & 0 deletions packages/codemods/src/transforms/v7/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function transformer(file: FileInfo, api: API, options: JSCodeShi

remapSizePropValue({
j,
api,
source,
componentName: localName,
sizesMap: {
Expand Down
29 changes: 29 additions & 0 deletions packages/codemods/src/transforms/v7/spacing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { API, FileInfo } from 'jscodeshift';
import { remapSizePropValue } from './common/remapSizePropValue';
import { getImportInfo } from '../../codemod-helpers';
import { JSCodeShiftOptions } from '../../types';

export const parser = 'tsx';

export default function transformer(file: FileInfo, api: API, options: JSCodeShiftOptions) {
const { alias } = options;
const j = api.jscodeshift;
const source = j(file.source);
const { localName } = getImportInfo(j, file, 'Spacing', alias);

if (!localName) {
return source.toSource();
}

remapSizePropValue({
j,
api,
source,
componentName: localName,
sizesMap: {
'3xs': '2xs',
},
});

return source.toSource();
}
1 change: 1 addition & 0 deletions packages/codemods/src/transforms/v7/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function transformer(file: FileInfo, api: API, options: JSCodeShi

remapSizePropValue({
j,
api,
source,
componentName: localName,
sizesMap: {
Expand Down
2 changes: 1 addition & 1 deletion packages/vkui/src/components/Header/Header.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
.Header__before {
align-self: center;
margin-inline-end: var(--vkui--spacing_size_m);
margin-block-start: var(--vkui--spacing_size_3xs);
margin-block-start: var(--vkui--spacing_size_2xs);
}

.Header__before--withSubtitle {
Expand Down
5 changes: 0 additions & 5 deletions packages/vkui/src/components/Spacing/Spacing.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
box-sizing: border-box;
}

.Spacing--3xs {
/* TODO [>=7]: удалить deprecated токен */
--vkui_internal--Spacing_gap: var(--vkui--spacing_size_3xs);
}

.Spacing--2xs {
--vkui_internal--Spacing_gap: var(--vkui--spacing_size_2xs);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/vkui/src/components/Spacing/Spacing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import styles from './Spacing.module.css';
export const CUSTOM_CSS_TOKEN_FOR_USER_GAP = '--vkui_internal--Spacing_gap';

export const sizesClassNames: Record<SpacingSize, string> = {
'3xs': styles['Spacing--3xs'],
'2xs': styles['Spacing--2xs'],
'xs': styles['Spacing--xs'],
's': styles['Spacing--s'],
Expand All @@ -19,7 +18,7 @@ export const sizesClassNames: Record<SpacingSize, string> = {
'4xl': styles['Spacing--4xl'],
};

export type SpacingSize = 's' | 'm' | 'l' | '3xs' | '2xs' | 'xs' | 'xl' | '2xl' | '3xl' | '4xl';
export type SpacingSize = 's' | 'm' | 'l' | '2xs' | 'xs' | 'xl' | '2xl' | '3xl' | '4xl';

export interface SpacingProps extends HTMLAttributesWithRootRef<HTMLDivElement> {
/**
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 491772f

Please sign in to comment.