Skip to content

Commit

Permalink
feat(ui-system): adding prop "className" to ThemeProvider (#465)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced the `ThemeProvider` component for better styling flexibility
by allowing dynamic class assignment and introducing a new `className`
prop.
- **Tests**
- Added tests to verify both default and custom classes functionality in
the `ThemeProvider` component.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
aversini authored Apr 6, 2024
1 parent ef63ccb commit 095531d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/ui-system/src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const FLEXGRID_CLASSNAME = "av-flexgrid";
export const FLEXGRID_ITEM_CLASSNAME = "av-flexgrid-item";
export const THEMEPROVIDER_CLASSNAME = "av-theme-provider";

export const FLEXGRID_GAP_RATIO = 0.25;
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import clsx from "clsx";
import { useEffect, useRef } from "react";

import { THEMEPROVIDER_CLASSNAME } from "../../common/constants";
import { ThemeProviderProps } from "./ThemeProviderTypes";

export const ThemeProvider = ({
customTheme,
children,
global,
className,
}: ThemeProviderProps) => {
const wrapperRef = useRef<HTMLDivElement>(null);
const wrapperClass = clsx(THEMEPROVIDER_CLASSNAME, "contents", className);

useEffect(() => {
const wrapper = global
Expand All @@ -22,7 +26,7 @@ export const ThemeProvider = ({
}, [customTheme, global]);

return customTheme || !global ? (
<div ref={wrapperRef} className="contents">
<div ref={wrapperRef} className={wrapperClass}>
{children}
</div>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export type ThemeProviderProps = {
* The children to render.
*/
children: React.ReactNode;
/**
* CSS class(es) to add to the main component wrapper.
*/
className?: string;
/**
* An object specifying custom properties impacting the base theme.
* Not all custom properties need to be specified.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { act, render, screen } from "@testing-library/react";

import { expectToHaveClasses } from "../../../../../../configuration/tests-helpers";
import { THEMEPROVIDER_CLASSNAME } from "../../../common/constants";
import { ThemeProvider } from "../..";

const customTheme = {
Expand All @@ -13,6 +15,16 @@ describe("ThemeProvider (exceptions)", () => {
});
});

describe("ThemeProvider props tests", () => {
it("should have default and custom classes", async () => {
await act(async () => {
render(<ThemeProvider className="toto">Hello World</ThemeProvider>);
});
const node = await screen.findByText("Hello World");
expectToHaveClasses(node, [THEMEPROVIDER_CLASSNAME, "contents", "toto"]);
});
});

describe("ThemeProvider injection tests", () => {
it("should NOT inject a custom theme locally [no custom theme]", async () => {
await act(async () => {
Expand Down

0 comments on commit 095531d

Please sign in to comment.