From 696000e07009fc900ade639cd95edb8821b68627 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Tue, 6 Aug 2024 15:44:27 -0500 Subject: [PATCH] refactor(Heading): update component to CSS Modules --- packages/react/src/Heading/Heading.module.css | 5 ++ packages/react/src/Heading/Heading.tsx | 27 +++++++++- .../src/Heading/__tests__/Heading.test.tsx | 54 ++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 packages/react/src/Heading/Heading.module.css diff --git a/packages/react/src/Heading/Heading.module.css b/packages/react/src/Heading/Heading.module.css new file mode 100644 index 00000000000..b7ef75e1d76 --- /dev/null +++ b/packages/react/src/Heading/Heading.module.css @@ -0,0 +1,5 @@ +.Heading { + font-weight: var(--base-text-weight-semibold); + font-size: var(--text-title-size-large); + margin: 0; +} diff --git a/packages/react/src/Heading/Heading.tsx b/packages/react/src/Heading/Heading.tsx index 048057004bd..5275a48fc17 100644 --- a/packages/react/src/Heading/Heading.tsx +++ b/packages/react/src/Heading/Heading.tsx @@ -1,3 +1,4 @@ +import cx from 'clsx' import React, {forwardRef, useEffect} from 'react' import styled from 'styled-components' import {get} from '../constants' @@ -6,6 +7,9 @@ import type {SxProp} from '../sx' import sx from '../sx' import type {ComponentProps} from '../utils/types' import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' +import classes from './Heading.module.css' +import {useFeatureFlag} from '../FeatureFlags' +import Box from '../Box' type StyledHeadingProps = { as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' @@ -17,7 +21,9 @@ const StyledHeading = styled.h2` margin: 0; ${sx}; ` -const Heading = forwardRef(({as: Component = 'h2', ...props}, forwardedRef) => { + +const Heading = forwardRef(({as: Component = 'h2', className, sx, ...props}, forwardedRef) => { + const enabled = useFeatureFlag('primer_react_css_modules') const innerRef = React.useRef(null) useRefObjectAsForwardedRef(forwardedRef, innerRef) @@ -37,9 +43,28 @@ const Heading = forwardRef(({as: Component = 'h2', ...props}, forwardedRef) => { }, [innerRef]) } + if (enabled) { + if (sx) { + return ( + + ) + } + + return + } + return ( { ), ).toHaveStyleRule('font-style', 'italic') }) + + describe('with primer_react_css_modules enabled', () => { + it('should only include css modules class', () => { + HTMLRender( + + test + , + ) + expect(screen.getByText('test')).toHaveClass('Heading') + // Note: this is the generated class name when styled-components is used + // for this component + expect(screen.getByText('test')).not.toHaveClass(/^Heading__StyledHeading/) + }) + + it('should support `className` on the outermost element', () => { + const {container} = HTMLRender( + + test + , + ) + expect(container.firstChild).toHaveClass('test') + }) + + it('should support overrides with sx if provided', () => { + HTMLRender( + + + test + + , + ) + + expect(screen.getByText('test')).toHaveStyle('font-weight: 900') + }) + }) })