From f7a16565a3bbc7f638bb4f93a2a826e9b76eed79 Mon Sep 17 00:00:00 2001 From: Charles de Dreuille Date: Sun, 15 Dec 2024 09:37:00 +0000 Subject: [PATCH] Add better support for breakpoints Signed-off-by: Charles de Dreuille --- packages/canon/.storybook/preview.tsx | 47 +++++++++++- .../src/components/Text/Text.stories.tsx | 10 +++ packages/canon/src/components/Text/Text.tsx | 13 +++- packages/canon/src/components/Text/types.ts | 11 ++- packages/canon/src/css/components.css | 1 + packages/canon/src/css/core.css | 10 +-- .../src/hooks/useIsomorphicLayoutEffect.ts | 19 +++++ packages/canon/src/hooks/useMediaQuery.ts | 76 +++++++++++++++++++ packages/canon/src/theme/context.tsx | 47 ++++++++++-- packages/canon/src/types.ts | 23 ++++++ .../canon/src/utils/getResponsiveValue.ts | 41 ++++++++++ 11 files changed, 279 insertions(+), 19 deletions(-) create mode 100644 packages/canon/src/hooks/useIsomorphicLayoutEffect.ts create mode 100644 packages/canon/src/hooks/useMediaQuery.ts create mode 100644 packages/canon/src/types.ts create mode 100644 packages/canon/src/utils/getResponsiveValue.ts diff --git a/packages/canon/.storybook/preview.tsx b/packages/canon/.storybook/preview.tsx index e78a9bb923..aa8f3feb0e 100644 --- a/packages/canon/.storybook/preview.tsx +++ b/packages/canon/.storybook/preview.tsx @@ -11,6 +11,7 @@ import '../src/css/components.css'; // Custom themes import './themes/backstage.css'; +import { ThemeProvider } from '../src/theme/context'; const preview: Preview = { parameters: { @@ -28,6 +29,46 @@ const preview: Preview = { method: 'alphabetical', }, }, + viewport: { + viewports: { + small: { + name: 'Small', + styles: { + width: '640px', + height: '100%', + }, + }, + medium: { + name: 'Medium', + styles: { + width: '768px', + height: '100%', + }, + }, + large: { + name: 'Large', + styles: { + width: '1024px', + height: '100%', + }, + }, + xlarge: { + name: 'XLarge', + styles: { + width: '1280px', + height: '100%', + }, + }, + '2xl': { + name: '2XL', + styles: { + width: '1536px', + height: '100%', + }, + }, + }, + defaultViewport: 'small', + }, }, decorators: [ withThemeByDataAttribute({ @@ -48,7 +89,11 @@ const preview: Preview = { 'var(--canon-background)'; }); - return ; + return ( + + + + ); }, ], }; diff --git a/packages/canon/src/components/Text/Text.stories.tsx b/packages/canon/src/components/Text/Text.stories.tsx index 5736cd1973..82d6514764 100644 --- a/packages/canon/src/components/Text/Text.stories.tsx +++ b/packages/canon/src/components/Text/Text.stories.tsx @@ -31,3 +31,13 @@ export const Default: Story = { ' Lorem ipsum dolor sit amet consectetur. Nec arcu vel lacus magna adipiscing nisi mauris tortor viverra. Enim rhoncus quisque consectetur ligula diam ac lacus massa. Id interdum id pellentesque justo ut massa nibh amet. Odio massa in scelerisque tortor massa integer purus amet enim. Eros sit neque nullam facilisis. Purus massa dignissim aliquet purus eu in. Urna consequat ullamcorper arcu amet dictumst. Commodo praesent turpis fringilla tristique congue volutpat in. Nulla in nulla ultrices lacus. In ultrices id tellus ut. Semper duis in in non proin et pulvinar. Sit amet vulputate lorem sit ipsum quis amet pulvinar ultricies. Imperdiet enim molestie habitant eget. Aenean mollis aenean dolor sodales tempus blandit.', }, }; + +export const Responsive: Story = { + args: { + ...Default.args, + variant: { + xs: 'label', + md: 'body', + }, + }, +}; diff --git a/packages/canon/src/components/Text/Text.tsx b/packages/canon/src/components/Text/Text.tsx index 6567eb989d..1c718786fa 100644 --- a/packages/canon/src/components/Text/Text.tsx +++ b/packages/canon/src/components/Text/Text.tsx @@ -16,6 +16,8 @@ import React, { forwardRef } from 'react'; import { TextProps } from './types'; +import { useTheme } from '../../theme/context'; +import { getResponsiveValue } from '../../utils/getResponsiveValue'; export const Text = forwardRef( (props, ref) => { @@ -26,13 +28,18 @@ export const Text = forwardRef( ...restProps } = props; + const { breakpoint } = useTheme(); + + const responsiveVariant = getResponsiveValue(variant, breakpoint); + const responsiveWeight = getResponsiveValue(weight, breakpoint); + return (

{children}

diff --git a/packages/canon/src/components/Text/types.ts b/packages/canon/src/components/Text/types.ts index e63b66e2d9..b054fdf2cf 100644 --- a/packages/canon/src/components/Text/types.ts +++ b/packages/canon/src/components/Text/types.ts @@ -14,9 +14,16 @@ * limitations under the License. */ +import { Breakpoint } from '../../layout/types'; + /** @public */ export interface TextProps { children: React.ReactNode; - variant?: 'subtitle' | 'body' | 'caption' | 'label'; - weight?: 'regular' | 'bold'; + variant?: + | 'subtitle' + | 'body' + | 'caption' + | 'label' + | Partial>; + weight?: 'regular' | 'bold' | Partial>; } diff --git a/packages/canon/src/css/components.css b/packages/canon/src/css/components.css index 1bd36990fd..ed602b7ab7 100644 --- a/packages/canon/src/css/components.css +++ b/packages/canon/src/css/components.css @@ -24,3 +24,4 @@ @import '../components/Checkbox/styles.css'; @import '../components/Table/styles.css'; @import '../components/Text/styles.css'; +@import '../components/Heading/styles.css'; diff --git a/packages/canon/src/css/core.css b/packages/canon/src/css/core.css index 80fe6e78f9..af1c261cc3 100644 --- a/packages/canon/src/css/core.css +++ b/packages/canon/src/css/core.css @@ -65,12 +65,12 @@ body { --canon-font-size-caption: 0.75rem; /* 12px */ --canon-font-size-body: 0.875rem; /* 14px */ --canon-font-size-subtitle: 1rem; /* 16px */ - --canon-font-size-title1: 1.25rem; /* 20px */ - --canon-font-size-title2: 1.5rem; /* 24px */ + --canon-font-size-title5: 1.25rem; /* 20px */ + --canon-font-size-title4: 1.5rem; /* 24px */ --canon-font-size-title3: 2rem; /* 32px */ - --canon-font-size-title4: 3rem; /* 48px */ - --canon-font-size-title5: 4rem; /* 64px */ - --canon-font-size-display: 5.75rem; /* 48px */ + --canon-font-size-title2: 3rem; /* 48px */ + --canon-font-size-title1: 4rem; /* 64px */ + --canon-font-size-display: 5.75rem; /* 92px */ /* Spacing */ --canon-spacing-5xs: 0.125rem; /* 2px */ diff --git a/packages/canon/src/hooks/useIsomorphicLayoutEffect.ts b/packages/canon/src/hooks/useIsomorphicLayoutEffect.ts new file mode 100644 index 0000000000..25ea45124e --- /dev/null +++ b/packages/canon/src/hooks/useIsomorphicLayoutEffect.ts @@ -0,0 +1,19 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { useEffect, useLayoutEffect } from 'react'; + +export const useIsomorphicLayoutEffect = + typeof window !== 'undefined' ? useLayoutEffect : useEffect; diff --git a/packages/canon/src/hooks/useMediaQuery.ts b/packages/canon/src/hooks/useMediaQuery.ts new file mode 100644 index 0000000000..9e2cc9ef0b --- /dev/null +++ b/packages/canon/src/hooks/useMediaQuery.ts @@ -0,0 +1,76 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { useState } from 'react'; + +import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'; + +type UseMediaQueryOptions = { + defaultValue?: boolean; + initializeWithValue?: boolean; +}; + +const IS_SERVER = typeof window === 'undefined'; + +export function useMediaQuery( + query: string, + { + defaultValue = false, + initializeWithValue = true, + }: UseMediaQueryOptions = {}, +): boolean { + const getMatches = (query: string): boolean => { + if (IS_SERVER) { + return defaultValue; + } + return window.matchMedia(query).matches; + }; + + const [matches, setMatches] = useState(() => { + if (initializeWithValue) { + return getMatches(query); + } + return defaultValue; + }); + + // Handles the change event of the media query. + function handleChange() { + setMatches(getMatches(query)); + } + + useIsomorphicLayoutEffect(() => { + const matchMedia = window.matchMedia(query); + + // Triggered at the first client-side load and if query changes + handleChange(); + + // Use deprecated `addListener` and `removeListener` to support Safari < 14 (#135) + if (matchMedia.addListener) { + matchMedia.addListener(handleChange); + } else { + matchMedia.addEventListener('change', handleChange); + } + + return () => { + if (matchMedia.removeListener) { + matchMedia.removeListener(handleChange); + } else { + matchMedia.removeEventListener('change', handleChange); + } + }; + }, [query]); + + return matches; +} diff --git a/packages/canon/src/theme/context.tsx b/packages/canon/src/theme/context.tsx index 77f43927f2..196c8fbb48 100644 --- a/packages/canon/src/theme/context.tsx +++ b/packages/canon/src/theme/context.tsx @@ -17,28 +17,59 @@ import React, { createContext, useContext, ReactNode } from 'react'; import { IconMap, IconNames } from '../components/Icon/types'; import { defaultIcons } from '../components/Icon/icons'; +import { useMediaQuery } from '../hooks/useMediaQuery'; +import { Breakpoints } from '../types'; + +const defaultBreakpoints: Breakpoints = { + xs: '0px', + sm: '640px', + md: '768px', + lg: '1024px', + xl: '1280px', + '2xl': '1536px', +}; interface ThemeContextProps { icons: IconMap; + breakpoint: keyof Breakpoints; } const ThemeContext = createContext({ icons: defaultIcons, + breakpoint: 'md', }); +interface ThemeProviderProps { + children?: ReactNode; + overrides?: Partial>; + breakpoints?: Partial; +} + /** @public */ -export const ThemeProvider = ({ - children, - overrides, -}: { - children: ReactNode; - overrides: Partial>; -}) => { +export const ThemeProvider = (props: ThemeProviderProps) => { + const { children, overrides, breakpoints = defaultBreakpoints } = props; + // Merge provided overrides with default icons const combinedIcons = { ...defaultIcons, ...overrides }; + const isBreakpointSm = useMediaQuery(`(min-width: ${breakpoints.sm})`); + const isBreakpointMd = useMediaQuery(`(min-width: ${breakpoints.md})`); + const isBreakpointLg = useMediaQuery(`(min-width: ${breakpoints.lg})`); + const isBreakpointXl = useMediaQuery(`(min-width: ${breakpoints.xl})`); + const isBreakpoint2xl = useMediaQuery(`(min-width: ${breakpoints['2xl']})`); + + // Determine the current breakpoint + const breakpoint = (() => { + if (isBreakpoint2xl) return '2xl'; + if (isBreakpointXl) return 'xl'; + if (isBreakpointLg) return 'lg'; + if (isBreakpointMd) return 'md'; + if (isBreakpointSm) return 'sm'; + return 'xs'; + })(); + return ( - + {children} ); diff --git a/packages/canon/src/types.ts b/packages/canon/src/types.ts new file mode 100644 index 0000000000..236d633f84 --- /dev/null +++ b/packages/canon/src/types.ts @@ -0,0 +1,23 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export interface Breakpoints { + xs: string; + sm: string; + md: string; + lg: string; + xl: string; + '2xl': string; +} diff --git a/packages/canon/src/utils/getResponsiveValue.ts b/packages/canon/src/utils/getResponsiveValue.ts new file mode 100644 index 0000000000..858d0c8bcb --- /dev/null +++ b/packages/canon/src/utils/getResponsiveValue.ts @@ -0,0 +1,41 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Breakpoints } from '../types'; + +export const getResponsiveValue = ( + value: any, + breakpoint: keyof Breakpoints, +) => { + if (typeof value === 'object') { + const breakpointsOrder: (keyof Breakpoints)[] = [ + 'xs', + 'sm', + 'md', + 'lg', + 'xl', + '2xl', + ]; + const index = breakpointsOrder.indexOf(breakpoint); + + for (let i = index; i >= 0; i--) { + if (value[breakpointsOrder[i]]) { + return value[breakpointsOrder[i]]; + } + } + return value['xs']; + } + return value; +};