From 8ac26005de49831b1322e8bcdc3b4334e802a408 Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Fri, 6 Dec 2024 19:34:31 +0100 Subject: [PATCH] Improve Container for custom breakpoints --- .../components/src/Container/Container.tsx | 73 +++++++++---------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/packages/components/src/Container/Container.tsx b/packages/components/src/Container/Container.tsx index 980f62f..a4d3ba3 100644 --- a/packages/components/src/Container/Container.tsx +++ b/packages/components/src/Container/Container.tsx @@ -1,6 +1,7 @@ 'use client'; -import { clamp, max, min, styled, useOwnerState } from '@react-universal/core'; +import { clamp, max, styled, useOwnerState } from '@react-universal/core'; +import type { Breakpoint } from '@react-universal/core'; import type { AnyObject } from '@react-universal/utils'; import { forwardRef } from 'react'; import { View } from '../View'; @@ -14,49 +15,43 @@ import type { const MIN_WIDTH = '20rem'; const MAX_WIDTH = '79.75rem'; -const ContainerRoot = styled(View)<{ ownerState: ContainerOwnerState }>( - ({ ownerState, runtime, theme }) => ({ - marginInline: 'auto', - paddingInline: { - xs: max(theme.space[4], runtime.insets.left, runtime.insets.right), - sm: max(theme.space[6], runtime.insets.left, runtime.insets.right), - md: max(theme.space[7], runtime.insets.left, runtime.insets.right), - }, - width: '100%', - variants: [ - { - props: ({ maxWidth }) => maxWidth != null && maxWidth !== false, - style: { - maxWidth: - ownerState.maxWidth != null && ownerState.maxWidth !== false - ? { - [ownerState.maxWidth]: - ownerState.maxWidth === 'xs' - ? max(theme.breakpoints.xs, MIN_WIDTH) - : min(theme.breakpoints[ownerState.maxWidth], MAX_WIDTH), - } - : undefined, - }, - }, - { - props: { fixed: true }, +const ContainerRoot = styled(View)<{ ownerState: ContainerOwnerState }>(({ runtime, theme }) => ({ + marginInline: 'auto', + paddingInline: { + xs: max(theme.space[4], runtime.insets.left, runtime.insets.right), + sm: max(theme.space[6], runtime.insets.left, runtime.insets.right), + md: max(theme.space[7], runtime.insets.left, runtime.insets.right), + }, + width: '100%', + variants: [ + ...(Object.entries(theme.breakpoints) as [Breakpoint, number][]).map( + ([breakpoint, maxWidth]) => ({ + props: { maxWidth: breakpoint }, style: { - maxWidth: Object.entries(theme.breakpoints).reduce( - (acc, [breakpoint, maxWidth]) => { - acc[breakpoint] = clamp(MIN_WIDTH, maxWidth, MAX_WIDTH); - return acc; - }, - {}, - ), - minWidth: 0, + maxWidth: { + [breakpoint]: clamp(MIN_WIDTH, maxWidth, MAX_WIDTH), + }, }, + }), + ), + { + props: { fixed: true }, + style: { + maxWidth: Object.entries(theme.breakpoints).reduce( + (acc, [breakpoint, maxWidth]) => { + acc[breakpoint] = clamp(MIN_WIDTH, maxWidth, MAX_WIDTH); + return acc; + }, + {}, + ), + minWidth: 0, }, - ], - }), -); + }, + ], +})); export const Container = forwardRef( - ({ fixed = false, maxWidth = 'lg', ...props }, ref) => { + ({ fixed = false, maxWidth = false, ...props }, ref) => { const ownerState = useOwnerState({ fixed, maxWidth,