From 84d5f03846ba335aba0a033ce0658a9f8d8a6106 Mon Sep 17 00:00:00 2001 From: Nick Grosenbacher Date: Mon, 16 Dec 2024 12:49:23 -0500 Subject: [PATCH] SWC-7166 - Add utility to properly spread Sx props - Fix `display` of OrientationBanner - Use utility in components spreading sx --- .../src/assets/mui_components/Kinomics.tsx | 6 ++-- .../src/assets/mui_components/Proteomics.tsx | 6 ++-- .../AccessApprovalCheckMark.tsx | 11 +++--- .../FullWidthAlert/FullWidthAlert.tsx | 23 +++++++------ .../components/Markdown/MarkdownPopover.tsx | 6 ++-- .../OrientationBanner/OrientationBanner.tsx | 24 ++++++------- .../src/theme/spreadSx.test.ts | 34 +++++++++++++++++++ .../src/theme/spreadSx.ts | 14 ++++++++ 8 files changed, 89 insertions(+), 35 deletions(-) create mode 100644 packages/synapse-react-client/src/theme/spreadSx.test.ts create mode 100644 packages/synapse-react-client/src/theme/spreadSx.ts diff --git a/packages/synapse-react-client/src/assets/mui_components/Kinomics.tsx b/packages/synapse-react-client/src/assets/mui_components/Kinomics.tsx index ab80e2d193..f73c01b48a 100644 --- a/packages/synapse-react-client/src/assets/mui_components/Kinomics.tsx +++ b/packages/synapse-react-client/src/assets/mui_components/Kinomics.tsx @@ -1,15 +1,15 @@ import SvgIcon, { SvgIconProps } from '@mui/material/SvgIcon' +import { spreadSx } from '../../theme/spreadSx' const Kinomics = (props: SvgIconProps) => { const { sx } = props return ( { const { sx } = props return ( = theme => ({ + [theme.breakpoints.down('md')]: { + display: 'none', + }, backgroundColor: '#F9FAFB', border: 'none', boxShadow: '0px 4px 4px rgba(0, 0, 0, 0.05)', @@ -65,7 +68,7 @@ function OrientationBanner(props: OrientationBannerProps) { paddingLeft: 4, '.MuiAlert-icon': { mr: 5 }, '.MuiAlertTitle-root': { color: theme.palette.grey[1000] }, - } + }) const BannerIllustration = Illustrations[name] return ( @@ -83,10 +86,7 @@ function OrientationBanner(props: OrientationBannerProps) { } setShowBanner(false) }} - sx={{ - ...defaultSx, - ...sx, - }} + sx={spreadSx(defaultSx, sx)} /> ) } diff --git a/packages/synapse-react-client/src/theme/spreadSx.test.ts b/packages/synapse-react-client/src/theme/spreadSx.test.ts new file mode 100644 index 0000000000..49abdc5993 --- /dev/null +++ b/packages/synapse-react-client/src/theme/spreadSx.test.ts @@ -0,0 +1,34 @@ +import { SxProps, Theme } from '@mui/material' +import { spreadSx } from './spreadSx' + +describe('spreadSx', () => { + const sxObject: SxProps = { color: 'red' } + const nestedSxObject: SxProps = [{ color: 'blue' }] + const sxFunction: SxProps = theme => ({ + color: theme.palette.primary.main, + }) + const nestedSxFunction: SxProps = [ + theme => ({ color: theme.palette.secondary.main }), + ] + + it('Removes undefined sx', () => { + expect(spreadSx(sxObject, undefined)).toEqual([sxObject]) + }) + it('Flattens a nested array', () => { + expect(spreadSx(sxObject, nestedSxObject)).toEqual([ + sxObject, + nestedSxObject[0], + ]) + }) + it('Handles a mix of functions and objects', () => { + expect( + spreadSx( + sxObject, + nestedSxObject, + sxFunction, + nestedSxFunction, + undefined, + ), + ).toEqual([sxObject, nestedSxObject[0], sxFunction, nestedSxFunction[0]]) + }) +}) diff --git a/packages/synapse-react-client/src/theme/spreadSx.ts b/packages/synapse-react-client/src/theme/spreadSx.ts new file mode 100644 index 0000000000..283552c183 --- /dev/null +++ b/packages/synapse-react-client/src/theme/spreadSx.ts @@ -0,0 +1,14 @@ +import { Theme } from '@mui/material' +import { SxProps } from '@mui/system' + +/** + * Utility to combine multiple SxProps into a single SxProps object that can be passed to a component. + * + * See https://github.com/mui/material-ui/issues/29274#issuecomment-953980228 + * @param sxProps + */ +export function spreadSx( + ...sxProps: (SxProps | undefined)[] +): SxProps { + return sxProps.filter(sx => sx !== undefined).flat() +}