Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(misc): CSS harcoded variables and classes replaced with react-tokens and react-styles #9266

Merged
merged 21 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0e906c9
refactor(misc): replace hardcoded css variables with tokens
adamviktora Jun 8, 2023
c84b0c7
refactor(misc): replace hardcoded CSS classes with react-styles
adamviktora Jun 12, 2023
846cb48
test(misc): update snapshots to use pf-v5-u-screen-reader
adamviktora Jun 21, 2023
a88b0f5
fix(demos): proper imports in .md files
adamviktora Jun 21, 2023
bdb2910
refactor(token names): rename tokens to pass build:docs framework and…
adamviktora Jun 22, 2023
e522621
refactor(token names): rename tokens to pass yarn docs:build, commit …
adamviktora Jun 22, 2023
d95501d
refactor: use whole imports to not blow up CJS bundle sizes
adamviktora Jun 22, 2023
190e2fb
fix: eslint-disable camel case
adamviktora Jun 22, 2023
124f14a
feat(misc): add imports to .md demos to pass docs build
adamviktora Jun 22, 2023
a925b4b
fix: use pf-v5-screen-reader class
adamviktora Aug 31, 2023
92e93a7
Merge branch 'main' into css-hardcoded-fix
adamviktora Aug 31, 2023
87b5ed3
fix: update test snapshots
adamviktora Aug 31, 2023
be766f5
formatted with prettier
adamviktora Aug 31, 2023
1850fb2
refactor(Alert): rename styles to buttonStyles
adamviktora Aug 31, 2023
f1c55ca
docs(Drawer): update Drawer example to use Title
adamviktora Sep 20, 2023
6652e2b
Merge branch 'main' into css-hardcoded-fix
adamviktora Sep 20, 2023
c60983b
docs(Wizard): remove custom color in custom nav item example
adamviktora Sep 20, 2023
5e1c040
lint error fix
adamviktora Sep 20, 2023
b0ce03e
Merge branch 'main' into css-hardcoded-fix
adamviktora Sep 27, 2023
30b579d
Merge branch 'main' into css-hardcoded-fix
adamviktora Oct 11, 2023
85850e8
refactor(misc): rename styles and other imports
adamviktora Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/CodeEditor/code-editor';
import fileUploadStyles from '@patternfly/react-styles/css/components/FileUpload/file-upload';
import {
Button,
ButtonVariant,
Expand Down Expand Up @@ -609,7 +610,7 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
}
{<div className={css(styles.codeEditorHeaderMain)}>{headerMainContent}</div>}
{!!shortcutsPopoverProps.bodyContent && (
<div className="pf-v5-c-code-editor__keyboard-shortcuts">
<div className={`${styles.codeEditor}__keyboard-shortcuts`}>
<Popover {...shortcutsPopoverProps}>
<Button variant={ButtonVariant.link} icon={<HelpIcon />}>
{shortcutsPopoverButtonText}
Expand Down Expand Up @@ -651,7 +652,9 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
{...getRootProps({
onClick: (event) => event.stopPropagation() // Prevents clicking TextArea from opening file dialog
})}
className={`pf-v5-c-file-upload ${isDragActive && 'pf-m-drag-hover'} ${isLoading && 'pf-m-loading'}`}
className={`${fileUploadStyles.fileUpload} ${isDragActive && fileUploadStyles.modifiers.dragHover} ${
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcoker shouldn't these styles be available in the code editor stylesheet rather than also needing to load file upload stylesheet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlabaj good question! Nope we don't include the file upload component styles in the code editor. AFAIK we don't do that for any component - a component only includes its styles. As far as I can tell, the only reason file upload styles are being imported is to get the loading state, which is not supported in the code editor. I don't think the way we're applying the file upload styles here is ideal though, I'm surprised it works the way it is 😅 I suppose it's worth considering if we need the loading styles at all? And if so, I suppose we should add them to the code editor. It should be a really simple addition.

We have since introduced the multiple file upload component, which mimics the empty state in its presentation. I wonder if that might be a more suitable component to use here instead of the empty state for uploading a file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcoker I will open a follow up issue to reconsider this implementation

isLoading && fileUploadStyles.modifiers.loading
}`}
>
{editorHeader}
<div className={css(styles.codeEditorMain)}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const AboutModalBoxContent: React.FunctionComponent<AboutModalBoxContentP
...props
}: AboutModalBoxContentProps) => (
<div className={css(styles.aboutModalBoxContent)} {...props}>
<div className={css('pf-v5-c-about-modal-box__body')}>
<div className={css(`${styles.aboutModalBox}__body`)}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this change is needed. THe reason the class is hard coded here is because it is a placeholder and has no associated styles

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefit to making changes like this one, as the versioned prefixes change over time, we won't need to come back and change all these hard coded strings - they will update automatically.

{hasNoContentContainer ? children : <div className={css(contentStyles.content)}>{children}</div>}
</div>
<p className={css(styles.aboutModalBoxStrapline)}>{trademark}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ propComponents: ['AboutModal']
---
import brandImg from '../../assets/brandImg.svg';
import bgImg from '../../assets/patternfly-orb.svg';
import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing';

## Examples
### Basic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { AboutModal, Alert, Button, TextContent, TextList, TextListItem } from '@patternfly/react-core';
import brandImg from '../../assets/brandImg.svg';
import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing';

export const AboutModalComplexUserPositionedContent: React.FunctionComponent = () => {
const [isModalOpen, setIsModalOpen] = React.useState(false);
Expand All @@ -24,12 +25,12 @@ export const AboutModalComplexUserPositionedContent: React.FunctionComponent = (
hasNoContentContainer={true}
productName="Product Name"
>
<TextContent id="test1" className="pf-v5-u-py-xl">
<TextContent id="test1" className={spacing.pyXl}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be more meaningful for the consumer to see the actual class name in the example.
@mcoker what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an issue in org to improve our documentation around react-tokens and react-styles so that products can better utilize them. I'd personally advocate for demonstrating how to use them well since this will reduce the work required to migrate to new major versions of PF. This avoids the hardcoding of strings with versioned prefixes.

<h4>About</h4>
<p>Content here</p>
</TextContent>
<Alert variant="info" title="Updates available" />
<TextContent id="test2" className="pf-v5-u-py-xl">
<TextContent id="test2" className={spacing.pyXl}>
<TextList component="dl">
<TextListItem component="dt">CFME Version</TextListItem>
<TextListItem component="dd">5.5.3.4.20102789036450</TextListItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/react';

import { Accordion } from '../Accordion';
import { AccordionContext } from '../AccordionContext';
import styles from '@patternfly/react-styles/css/components/Accordion/accordion';

test('Renders without children', () => {
render(<Accordion data-testid="accordion" />);
Expand Down Expand Up @@ -34,10 +35,10 @@ test('Renders with inherited element props spread to the component', () => {
expect(screen.getByText('Test')).toHaveAccessibleName('Label');
});

test('Renders with class name pf-v5-c-accordion', () => {
test(`Renders with class name ${styles.accordion}`, () => {
render(<Accordion>Test</Accordion>);

expect(screen.getByText('Test')).toHaveClass('pf-v5-c-accordion');
expect(screen.getByText('Test')).toHaveClass(styles.accordion);
});

test('Renders with custom class names provided via prop', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/react';

import { AccordionContent } from '../AccordionContent';
import { AccordionContext } from '../AccordionContext';
import styles from '@patternfly/react-styles/css/components/Accordion/accordion';

jest.mock('../AccordionExpandableContentBody', () => ({
AccordionExpandableContentBody: ({ children }) => <div aria-label="Expanded content body mock">{children}</div>
Expand Down Expand Up @@ -85,14 +86,14 @@ test('Renders with inherited element props spread to the component', () => {
expect(screen.getByRole('heading')).toHaveAccessibleName('Label');
});

test('Renders with class name pf-v5-c-accordion__expandable-content', () => {
test(`Renders with class name ${styles.accordionExpandableContent}`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wise-king-sullyman, what do you think about updating the test like this? Will loading the stylesheets impact performance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might impact performance, I'm honestly not sure if it would really be noticeable or not.

I think it's worth trying though, if we can't do this or something like it we'll have to update all of the classnames each major release, which isn't ideal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably minimal. They job looks like it still completes in 4 minutes.

render(
<AccordionContext.Provider value={{ ContentContainer: 'h3' }}>
<AccordionContent>Test</AccordionContent>
</AccordionContext.Provider>
);

expect(screen.getByRole('heading')).toHaveClass('pf-v5-c-accordion__expandable-content');
expect(screen.getByRole('heading')).toHaveClass(styles.accordionExpandableContent);
});

test('Renders with custom class names provided via prop', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import { render, screen } from '@testing-library/react';

import { AccordionExpandableContentBody } from '../AccordionExpandableContentBody';
import styles from '@patternfly/react-styles/css/components/Accordion/accordion';

test('Renders without children', () => {
const { asFragment } = render(<AccordionExpandableContentBody />);
Expand All @@ -19,10 +20,10 @@ test('Renders children', () => {
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with class name pf-v5-c-accordion__expandable-content-body', () => {
test(`Renders with class name ${styles.accordionExpandableContentBody}`, () => {
render(<AccordionExpandableContentBody>Test</AccordionExpandableContentBody>);

expect(screen.getByText('Test')).toHaveClass('pf-v5-c-accordion__expandable-content-body');
expect(screen.getByText('Test')).toHaveClass(styles.accordionExpandableContentBody);
});

test('Matches the snapshot', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/react';

import { AccordionToggle } from '../AccordionToggle';
import { AccordionContext } from '../AccordionContext';
import styles from '@patternfly/react-styles/css/components/Accordion/accordion';

jest.mock('@patternfly/react-icons/dist/esm/icons/angle-right-icon', () => () => 'Icon mock');

Expand Down Expand Up @@ -42,7 +43,7 @@ test('Renders with inherited element props spread to the component', () => {
expect(screen.getByRole('button')).toHaveAccessibleName('Label');
});

test('Renders the accordion toggle with class pf-v5-c-accordion__toggle', () => {
test(`Renders the accordion toggle with class ${styles.accordionToggle}`, () => {
render(
<AccordionContext.Provider value={{ ToggleContainer: 'h3' }}>
<AccordionToggle id="accordion-toggle" aria-label="Accordion test">
Expand All @@ -51,7 +52,7 @@ test('Renders the accordion toggle with class pf-v5-c-accordion__toggle', () =>
</AccordionContext.Provider>
);

expect(screen.getByRole('button')).toHaveClass('pf-v5-c-accordion__toggle');
expect(screen.getByRole('button')).toHaveClass(styles.accordionToggle);
});

test('Renders the accordion toggle with custom class names provided via prop', () => {
Expand All @@ -66,7 +67,7 @@ test('Renders the accordion toggle with custom class names provided via prop', (
expect(screen.getByRole('button')).toHaveClass('test-class');
});

test('Renders with children inside class pf-v5-c-accordion__toggle-text', () => {
test(`Renders with children inside class ${styles.accordionToggleText}`, () => {
render(
<AccordionContext.Provider value={{ ToggleContainer: 'h3' }}>
<AccordionToggle id="accordion-toggle" aria-label="Accordion test">
Expand All @@ -75,10 +76,10 @@ test('Renders with children inside class pf-v5-c-accordion__toggle-text', () =>
</AccordionContext.Provider>
);

expect(screen.getByText('Test')).toHaveClass('pf-v5-c-accordion__toggle-text');
expect(screen.getByText('Test')).toHaveClass(styles.accordionToggleText);
});

test('Renders with the toggle icon inside class pf-v5-c-accordion__toggle-icon', () => {
test(`Renders with the toggle icon inside class ${styles.accordionToggleIcon}`, () => {
render(
<AccordionContext.Provider value={{ ToggleContainer: 'h3' }}>
<AccordionToggle id="accordion-toggle" aria-label="Accordion test">
Expand All @@ -87,7 +88,7 @@ test('Renders with the toggle icon inside class pf-v5-c-accordion__toggle-icon',
</AccordionContext.Provider>
);

expect(screen.getByText('Icon mock')).toHaveClass('pf-v5-c-accordion__toggle-icon');
expect(screen.getByText('Icon mock')).toHaveClass(styles.accordionToggleIcon);
});

test('Renders with the id prop passed to the toggle', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/ActionList/action-list';

export interface ActionListItemProps extends React.HTMLProps<HTMLDivElement> {
/** Children of the action list item */
Expand All @@ -13,7 +14,7 @@ export const ActionListItem: React.FunctionComponent<ActionListItemProps> = ({
className = '',
...props
}: ActionListItemProps) => (
<div className={css('pf-v5-c-action-list__item', className)} {...props}>
<div className={css(`${styles.actionList}__item`, className)} {...props}>
{children}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { ActionList } from '../ActionList';
import styles from '@patternfly/react-styles/css/components/ActionList/action-list';

test('Renders without children', () => {
render(<ActionList data-testid="action-list" />);
Expand All @@ -13,10 +14,10 @@ test('Renders children', () => {
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with class pf-v5-c-action-list', () => {
test(`Renders with class ${styles.actionList}`, () => {
render(<ActionList>Test</ActionList>);

expect(screen.getByText('Test')).toHaveClass('pf-v5-c-action-list');
expect(screen.getByText('Test')).toHaveClass(styles.actionList);
});

test('Renders with custom class names provided via prop', () => {
Expand All @@ -25,16 +26,16 @@ test('Renders with custom class names provided via prop', () => {
expect(screen.getByText('Test')).toHaveClass('custom-class');
});

test('Does not render with class pf-m-icons by default', () => {
test(`Does not render with class ${styles.modifiers.icons} by default`, () => {
render(<ActionList>Test</ActionList>);

expect(screen.getByText('Test')).not.toHaveClass('pf-m-icons');
expect(screen.getByText('Test')).not.toHaveClass(styles.modifiers.icons);
});

test('Renders with class pf-m-icons when isIconList is true', () => {
test(`Renders with class ${styles.modifiers.icons} when isIconList is true`, () => {
render(<ActionList isIconList={true}>Test</ActionList>);

expect(screen.getByText('Test')).toHaveClass('pf-m-icons');
expect(screen.getByText('Test')).toHaveClass(styles.modifiers.icons);
});

test('Renders with inherited element props spread to the component', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { ActionListGroup } from '../ActionListGroup';
import styles from '@patternfly/react-styles/css/components/ActionList/action-list';

test('Renders without children', () => {
render(<ActionListGroup data-testid="action-list-group" />);
Expand All @@ -13,10 +14,10 @@ test('Renders children', () => {
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with class pf-v5-c-action-list__group', () => {
test(`Renders with class ${styles.actionListGroup}`, () => {
render(<ActionListGroup>Test</ActionListGroup>);

expect(screen.getByText('Test')).toHaveClass('pf-v5-c-action-list__group');
expect(screen.getByText('Test')).toHaveClass(styles.actionListGroup);
});

test('Renders with custom class names provided via prop', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { ActionListItem } from '../ActionListItem';
import styles from '@patternfly/react-styles/css/components/ActionList/action-list';

test('Renders without children', () => {
render(<ActionListItem data-testid="action-list-item" />);
Expand All @@ -13,10 +14,10 @@ test('Renders children', () => {
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with class pf-v5-c-action-list__item', () => {
test(`Renders with class ${styles.actionList}__item`, () => {
render(<ActionListItem>Test</ActionListItem>);

expect(screen.getByText('Test')).toHaveClass('pf-v5-c-action-list__item');
expect(screen.getByText('Test')).toHaveClass(`${styles.actionList}__item`);
});

test('Renders with custom class names provided via prop', () => {
Expand Down
28 changes: 15 additions & 13 deletions packages/react-core/src/components/Alert/__tests__/Alert.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { act } from 'react-dom/test-utils';
import { Alert, AlertVariant } from '../Alert';
import { AlertContext } from '../AlertContext';
import { capitalize } from '../../../helpers';
import cssAlertTitleMaxLines from '@patternfly/react-tokens/dist/esm/c_alert__title_max_lines';
import styles from '@patternfly/react-styles/css/components/Alert/alert';

jest.mock('../AlertToggleExpandButton', () => ({
AlertToggleExpandButton: ({ isExpanded, onToggleExpand, ...props }) => (
Expand Down Expand Up @@ -34,18 +36,18 @@ test('Renders without children', () => {
expect(screen.getByTestId('container').firstChild).toBeVisible();
});

test('Renders with class pf-v5-c-alert on the containing div', () => {
test(`Renders with class ${styles.alert} on the containing div`, () => {
render(
<Alert title="Some title" data-testid="Alert-test-id">
Some alert
</Alert>
);
expect(screen.getByTestId('Alert-test-id')).toHaveClass('pf-v5-c-alert');
expect(screen.getByTestId('Alert-test-id')).toHaveClass(styles.alert);
});

test('Renders with class pf-v5-c-alert__title on the div containing the title', () => {
test(`Renders with class ${styles.alertTitle} on the div containing the title`, () => {
render(<Alert title="Some title">Some alert</Alert>);
expect(screen.getByRole('heading', { name: 'Custom alert: Some title' })).toHaveClass('pf-v5-c-alert__title');
expect(screen.getByRole('heading', { name: 'Custom alert: Some title' })).toHaveClass(styles.alertTitle);
});

test('Renders with Custom hidden text of "Custom alert:"', () => {
Expand Down Expand Up @@ -199,14 +201,14 @@ test('Renders the element passed via the actionClose prop', () => {
expect(screen.getByRole('button', { name: 'Action close' })).toBeVisible();
});

test('Renders the actionClose element inside pf-v5-c-alert__action', () => {
test(`Renders the actionClose element inside ${styles.alertAction}`, () => {
render(
<Alert title="Some title" actionClose="Action close">
Some alert
</Alert>
);

expect(screen.getByText('Action close')).toHaveClass('pf-v5-c-alert__action');
expect(screen.getByText('Action close')).toHaveClass(styles.alertAction);
});

test('Provides the actionClose element access to the title via a context', () => {
Expand Down Expand Up @@ -239,14 +241,14 @@ test('Renders the element passed via the actionLinks prop', () => {
expect(screen.getByRole('button', { name: 'Action link' })).toBeVisible();
});

test('Renders the actionLinks element inside pf-v5-c-alert__action-group', () => {
test(`Renders the actionLinks element inside ${styles.alertActionGroup}`, () => {
render(
<Alert title="Some title" actionLinks="Action link">
Some alert
</Alert>
);

expect(screen.getByText('Action link')).toHaveClass('pf-v5-c-alert__action-group');
expect(screen.getByText('Action link')).toHaveClass(styles.alertActionGroup);
});

test('Renders children', () => {
Expand All @@ -255,10 +257,10 @@ test('Renders children', () => {
expect(screen.getByText('Some alert')).toBeVisible();
});

test('Renders children inside pf-v5-c-alert__description', () => {
test(`Renders children inside ${styles.alertDescription}`, () => {
render(<Alert title="Some title">Some alert</Alert>);

expect(screen.getByText('Some alert')).toHaveClass('pf-v5-c-alert__description');
expect(screen.getByText('Some alert')).toHaveClass(styles.alertDescription);
});

test('Renders with the aria label passed via prop', () => {
Expand Down Expand Up @@ -578,7 +580,7 @@ test('Renders titles with the expected truncation styling when truncateTitle is
const title = screen.getByRole('heading');

expect(title).toHaveClass('pf-m-truncate');
expect(title).toHaveAttribute('style', '--pf-v5-c-alert__title--max-lines: 3;');
expect(title).toHaveAttribute('style', `${cssAlertTitleMaxLines.name}: 3;`);
});

test('Passes customIcon value to AlertIcon', () => {
Expand Down Expand Up @@ -609,14 +611,14 @@ test('Renders with class pf-m-expandable when isExpandable = true', () => {
expect(screen.getByTestId('Alert-test-id')).toHaveClass('pf-m-expandable');
});

test('Renders AlertToggleExpandButton inside pf-v5-c-alert__toggle', () => {
test(`Renders AlertToggleExpandButton inside ${styles.alertToggle}`, () => {
render(
<Alert isExpandable title="Some title">
Some alert
</Alert>
);

expect(screen.getByRole('button').parentElement).toHaveClass('pf-v5-c-alert__toggle');
expect(screen.getByRole('button').parentElement).toHaveClass(styles.alertToggle);
});

test('Does not render with class pf-m-expanded when AlertToggleExpandButton has not been clicked', () => {
Expand Down
Loading
Loading