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

feat(AspectRatio): extract ratio prop type #8088

Merged
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
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.
Loading