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

ThemeOverride #6

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
99 changes: 99 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions .yarn/versions/06d7e864.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
theme-party: minor
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function Link({ children }) {

```

### Override components
### Replace components ("costuming")

Create a "costumed" component containing your default markup.

Expand Down Expand Up @@ -158,7 +158,30 @@ function Page() {
}
```

## Releasing
### Theme overrides

Sometimes, you might want to override a single theme value, while inheriting the rest of the current theme.

```tsx
import { ThemeOverride, ThemePartyConfig } from 'theme-party';

const override: ThemePartyConfig = { color: { link: 'blue' } };
function Component() {
return (
<ThemeOverride value={override}>
<Link>Click me</Link>
</ThemeOverride>
);
}
```

This creates a temporary theme that preserves the rest of the values of the currently selected theme.

Note: the object passed to value must be stable across renders. Either use a constant or memoize the object.

## Contributing

### Releasing

In pull request branch, run

Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
testMatch: ['<rootDir>/src/**/*.test.ts?(x)'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
};

export default jestConfig;
1 change: 1 addition & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@rollup/plugin-commonjs": "^25.0.2",
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-typescript": "^11.1.1",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.14.195",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default [
plugins: [
resolve(),
commonjs(),
typescript({ tsconfig: './tsconfig.json' }),
typescript({ tsconfig: './tsconfig.build.json' }),
],
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { ThemeParty } from "./ThemeParty";
export { costumed } from "./react/costumed";
export { ThemeProvider } from "./react/ThemeProvider";
export { ThemeOverride } from "./react/ThemeOverride";
export { useTheme } from "./react/useTheme";
export { UserTheme } from "./types";
export { ThemeExtract } from './ThemeParty.types';
export { UserTheme, ThemeOfParty, DefaultTheme, DefaultThemeParty, ThemePartyConfig } from "./types";
export { ThemeExtract, ThemeConfig } from './ThemeParty.types';
26 changes: 26 additions & 0 deletions src/react/ThemeOverride.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useContext, useMemo } from 'react';
import { DefaultTheme, ThemePartyConfig } from '../types';
import { themePartyContext } from './context';

export interface ThemeOverrideProps<T extends {}> {
/** Override values of current theme. Object should be stable across renders */
value: ThemePartyConfig<T>;
children: React.ReactNode;
}

export function ThemeOverride<T extends {} = DefaultTheme>({
value,
children,
}: ThemeOverrideProps<T>) {
const themeParty = useContext(themePartyContext);
if (!themeParty)
throw new Error('ThemeOverride must be used within a ThemeProvider');

const override = useMemo(() => themeParty.createTheme(value), [themeParty, value]);

return (
<themePartyContext.Provider value={override}>
{children}
</themePartyContext.Provider>
);
}
47 changes: 47 additions & 0 deletions src/react/__tests__/ThemeOverride.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { render } from '@testing-library/react';
import { ThemeParty } from '../../ThemeParty';
import { useTheme } from '../useTheme';
import { ThemeProvider } from '../ThemeProvider';
import { ThemeOverride } from '../ThemeOverride';
import { ThemeOfParty } from '../../types';

const themeParty = new ThemeParty({
color: 'red',
fontWeight: 700,
});

type Theme = ThemeOfParty<typeof themeParty>;

const theme2 = themeParty.createTheme({
color: 'blue',
});

const Text = ({ children }: { children: React.ReactNode }) => {
const { color, fontWeight } = useTheme<typeof themeParty>();
return <p style={{ color, fontWeight }}>{children}</p>;
};

test('ThemeOverride', () => {
const { getByText } = render(
<ThemeProvider theme={theme2}>
<Text>Text 1</Text>

<ThemeOverride<Theme> value={{ color: 'green' }}>
<Text>Text 2</Text>
</ThemeOverride>

<ThemeOverride<Theme> value={{ fontWeight: () => 400 }}>
<Text>Text 3</Text>
</ThemeOverride>
</ThemeProvider>
);

expect(getByText('Text 1')).toHaveStyle('color: blue');
expect(getByText('Text 1')).toHaveStyle('font-weight: 700');

expect(getByText('Text 2')).toHaveStyle('color: green');
expect(getByText('Text 2')).toHaveStyle('font-weight: 700');

expect(getByText('Text 3')).toHaveStyle('color: blue');
expect(getByText('Text 3')).toHaveStyle('font-weight: 400');
});
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ThemeParty } from './ThemeParty';
import { ThemeExtract } from './ThemeParty.types';
import { DeepPartial, ThemeConfig, ThemeExtract } from './ThemeParty.types';

/**
* Allows user to define type of their custom theme object.
Expand All @@ -23,3 +23,5 @@ export type DefaultThemeParty = unknown extends UserTheme['default']
: UserTheme['default'];

export type DefaultTheme = ThemeOfParty<DefaultThemeParty>;

export type ThemePartyConfig<T extends {} = DefaultTheme> = ThemeConfig<ThemeExtract<T>, DeepPartial<ThemeExtract<T>>>;
5 changes: 5 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*"],
"exclude": ["**/__tests__/**"],
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
"include": ["src/**/*", "./jest.setup.ts"]
}
Loading
Loading