Skip to content

Commit e9fd3a3

Browse files
committed
Define and use CSSPropertiesWithVariables type
1 parent a477845 commit e9fd3a3

5 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/components/BodyPortal.spec.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import type { CSSPropertiesWithVariables } from '../types';
23
import { render } from '@testing-library/react';
34
import { BodyPortal } from './BodyPortal';
45
import { BodyPortalSlotsContext } from './BodyPortalSlotsContext';
@@ -266,7 +267,7 @@ describe('BodyPortal', () => {
266267
render(
267268
<BodyPortal
268269
slot='modal'
269-
style={{ '--my-variable': 'red', '--another-var': '10px' } as React.CSSProperties}
270+
style={{ '--my-variable': 'red', '--another-var': '10px' } as CSSPropertiesWithVariables}
270271
>
271272
Modal content
272273
</BodyPortal>,
@@ -300,7 +301,7 @@ describe('BodyPortal', () => {
300301
const TestComponent = ({ color }: { color: string }) => (
301302
<BodyPortal
302303
slot='modal'
303-
style={{ '--color': color } as React.CSSProperties}
304+
style={{ '--color': color } as CSSPropertiesWithVariables}
304305
>
305306
Modal content
306307
</BodyPortal>
@@ -326,7 +327,7 @@ describe('BodyPortal', () => {
326327
const { unmount } = render(
327328
<BodyPortal
328329
slot='modal'
329-
style={{ '--my-variable': 'red', backgroundColor: 'blue' } as React.CSSProperties}
330+
style={{ '--my-variable': 'red', backgroundColor: 'blue' } as CSSPropertiesWithVariables}
330331
>
331332
Modal content
332333
</BodyPortal>,
@@ -344,11 +345,11 @@ describe('BodyPortal', () => {
344345
expect(document.body.querySelector('[data-portal-slot="modal"]')).toBeNull();
345346
});
346347

347-
it('handles null and undefined style values gracefully', () => {
348+
it('handles undefined style values gracefully', () => {
348349
render(
349350
<BodyPortal
350351
slot='modal'
351-
style={{ '--defined': 'red', '--null': null, '--undefined': undefined } as React.CSSProperties}
352+
style={{ '--defined': 'red', '--undefined': undefined } as CSSPropertiesWithVariables}
352353
>
353354
Modal content
354355
</BodyPortal>,
@@ -359,7 +360,6 @@ describe('BodyPortal', () => {
359360
expect(portal).toBeTruthy();
360361
expect(portal.style.getPropertyValue('--defined')).toBe('red');
361362
// null and undefined values should not be set
362-
expect(portal.style.getPropertyValue('--null')).toBe('');
363363
expect(portal.style.getPropertyValue('--undefined')).toBe('');
364364
});
365365
});

src/components/BodyPortal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { createPortal } from 'react-dom';
33
import { BodyPortalSlotsContext } from './BodyPortalSlotsContext';
4+
import { CSSPropertiesWithVariables } from '../types';
45

56
const getInsertBeforeTarget = (bodyPortalSlots: string[], slot?: string) => {
67
// Note: If the slot is not found in bodyPortalSlots, this code will append the tag instead,
@@ -29,7 +30,7 @@ export type BodyPortalProps = React.PropsWithChildren<{
2930
id?: string;
3031
'data-testid'?: string;
3132
ariaLabel?: string;
32-
style?: React.CSSProperties;
33+
style?: CSSPropertiesWithVariables;
3334
}>;
3435

3536
export const BodyPortal = React.forwardRef<HTMLElement, BodyPortalProps>((

src/components/NavBar.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import type { CSSPropertiesWithVariables } from "../types";
23
import { colors } from "../theme";
34
import { NavBar } from "./NavBar";
45
import { NavBarButton } from "./NavBarButton";
@@ -16,7 +17,7 @@ const InfoMenuButton = ({ children, ...props }: React.ComponentProps<typeof NavB
1617
style={{
1718
'--info-icon-fill': colors.palette.lightBlue,
1819
...props.style
19-
} as React.CSSProperties}
20+
} as CSSPropertiesWithVariables}
2021
className="info-menu-button"
2122
>
2223
{children}

src/components/NavBar.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { CSSPropertiesWithVariables } from '../types';
23
import classNames from 'classnames';
34
import * as Constants from '../constants';
45
import theme from '../theme';
@@ -40,14 +41,14 @@ export const NavBar = ({
4041
'--navbar-padding-mobile': `${theme.padding.navbar.mobile}rem`,
4142
'--navbar-padding-desktop': `${theme.padding.navbar.desktop}rem`,
4243
...style
43-
} as React.CSSProperties;
44+
} as CSSPropertiesWithVariables;
4445

4546
const barStyle = {
4647
'--navbar-max-width': maxWidth ? `${maxWidth}rem` : undefined,
4748
'--navbar-justify-content': justifyContent,
4849
'--navbar-height-mobile': `${navMobileHeight || Constants.navMobileHeight}rem`,
4950
'--navbar-height-desktop': `${navDesktopHeight || Constants.navDesktopHeight}rem`,
50-
} as React.CSSProperties;
51+
} as CSSPropertiesWithVariables;
5152

5253
return (
5354
<BodyPortal

src/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CSSProperties } from "react";
2+
13
export type ToastData = {
24
id?: string;
35
title: string;
@@ -15,3 +17,7 @@ export type SentryError = {
1517
eventId?: string;
1618
type?: string;
1719
};
20+
21+
export type CSSPropertiesWithVariables = CSSProperties & {
22+
[key: `--${string}`]: string | number | undefined;
23+
};

0 commit comments

Comments
 (0)