Skip to content

Commit

Permalink
feat(AspectRatio): extract ratio prop type (#8088)
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuhamethanov authored Dec 16, 2024
1 parent 86648b8 commit 5cb14c4
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const AspectRatioPlayground = (props: ComponentPlaygroundProps) => {
{...props}
propSets={[
{
ratio: [1 / 1, 3 / 4, 16 / 9],
ratio: [1 / 1, 3 / 4, 16 / 9, '1', 'calc(16 / 9)'],
children: [<div key="" style={{ background: 'green' }} />],
mode: ['stretch'],
},
Expand Down
19 changes: 19 additions & 0 deletions packages/vkui/src/components/AspectRatio/AspectRatio.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { render, screen } from '@testing-library/react';
import { baselineComponent } from '../../testing/utils';
import { AspectRatio } from './AspectRatio';
import styles from './AspectRatio.module.css';

describe(AspectRatio, () => {
baselineComponent(AspectRatio);

it('check mode stretch has specific className', () => {
render(<AspectRatio data-testid="ratio" ratio={16 / 9} mode="stretch" />);
expect(screen.getByTestId('ratio')).toHaveClass(styles.modeStretch);
});

it('check using custom ratio', () => {
const { rerender } = render(
<AspectRatio data-testid="ratio" ratio="var(--custom-aspect-ratio-var)" />,
);
expect(screen.getByTestId('ratio')).toHaveStyle(
'--vkui_internal--aspect_ratio: var(--custom-aspect-ratio-var);',
);

rerender(<AspectRatio data-testid="ratio" ratio="calc(16 / 9)" />);
expect(screen.getByTestId('ratio')).toHaveStyle('--vkui_internal--aspect_ratio: calc(16 / 9);');
});
});
9 changes: 6 additions & 3 deletions packages/vkui/src/components/AspectRatio/AspectRatio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export interface AspectRatioProps extends Omit<RootComponentProps<HTMLElement>,
*/
mode?: 'stretch' | 'none';
/**
* Например 16 / 9, 4 / 3, 1920 / 1080
* Например:
* - в виде числа: 16 / 9, 4 / 3, 1920 / 1080,
* - в виде css переменной: `var(--css-aspect-ratio-var)`
* - в виде сложного выражения: `calc(<какие-то вычисления>)`
*/
ratio: number;
ratio: number | string;
}

/**
Expand All @@ -29,7 +32,7 @@ export function AspectRatio({
...props
}: AspectRatioProps): React.JSX.Element {
const style: React.CSSProperties & CSSCustomProperties = {
'--vkui_internal--aspect_ratio': String(ratio),
'--vkui_internal--aspect_ratio': typeof ratio === 'number' ? String(ratio) : ratio,
};

return (
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.
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.
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.
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 5cb14c4

Please sign in to comment.