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

fix(theming): allow theming of typescript consumers #984

Merged
merged 1 commit into from
Jan 14, 2021
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
8 changes: 4 additions & 4 deletions packages/theming/src/elements/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
*/

import React, { useRef } from 'react';
import { ThemeProvider, DefaultTheme, ThemeProps } from 'styled-components';
import { ThemeProvider, ThemeProps } from 'styled-components';
import { useFocusVisible } from '@zendeskgarden/container-focusvisible';
import { getControlledValue } from '@zendeskgarden/container-utilities';
import DEFAULT_THEME from './theme';
import DEFAULT_THEME, { IGardenTheme } from './theme';
import { useDocument } from '../utils/useDocument';

interface IGardenThemeProviderProps extends Partial<ThemeProps<DefaultTheme>> {
interface IGardenThemeProviderProps extends Partial<ThemeProps<IGardenTheme>> {
/**
* Provides values for component styling. See styled-components
* [`ThemeProvider`](https://styled-components.com/docs/api#themeprovider)
* for details.
*/
theme?: DefaultTheme;
theme?: IGardenTheme;
/**
* Provides a reference to the DOM node used to scope a `:focus-visible`
* polyfill. If left `undefined`, a scoping `<div>` will be rendered.
Expand Down
103 changes: 100 additions & 3 deletions packages/theming/src/elements/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,106 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import { DefaultTheme } from 'styled-components';
import PALETTE from '../palette';

type Hue = Record<number | string, string> | string;

export interface IGardenTheme {
rtl: boolean;
document?: any;
borders: {
sm: string;
md: string;
};
borderRadii: {
sm: string;
md: string;
};
borderStyles: {
solid: string;
};
borderWidths: {
sm: string;
md: string;
};
breakpoints: {
xs: string;
sm: string;
md: string;
lg: string;
xl: string;
};
colors: {
base: 'light' | 'dark';
background: string;
foreground: string;
primaryHue: string;
dangerHue: string;
warningHue: string;
successHue: string;
neutralHue: string;
chromeHue: string;
};
components: Record<string, any>;
fonts: {
mono: string;
system: string;
};
fontSizes: {
xs: string;
sm: string;
md: string;
lg: string;
xl: string;
xxl: string;
xxxl: string;
};
fontWeights: {
thin: number;
extralight: number;
light: number;
regular: number;
medium: number;
semibold: number;
bold: number;
extrabold: number;
black: number;
};
iconSizes: {
sm: string;
md: string;
lg: string;
};
lineHeights: {
sm: string;
md: string;
lg: string;
xl: string;
xxl: string;
xxxl: string;
};
shadowWidths: {
sm: string;
md: string;
};
shadows: {
sm: (color: string) => string;
md: (color: string) => string;
lg: (offsetY: string, blurRadius: string, color: string) => string;
};
space: {
base: number;
xxs: string;
xs: string;
sm: string;
md: string;
lg: string;
xl: string;
xxl: string;
};
palette: Record<string, Hue>;
}

const BASE = 4;

const borderRadii = {
Expand Down Expand Up @@ -112,7 +209,7 @@ const lineHeights = {
const palette = { ...PALETTE };

/* Exclude product palette from the theme */
delete palette.product;
delete (palette as any).product;
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 another way around this without using any is...

const { ...palette, product } = PALETTE;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since PALETTE is inferring its type this can cause a bunch of Index signature is missing in type errors. The original ... operator is what keeps this working.

We probably shouldn't be inferring those types anyway, but that is probably a larger scope change than we should do here.


const shadowWidths = {
sm: '2px',
Expand All @@ -137,7 +234,7 @@ const space = {
xxl: `${BASE * 12}px`
};

const DEFAULT_THEME: DefaultTheme = {
const DEFAULT_THEME: IGardenTheme = {
borders,
borderRadii,
borderStyles,
Expand Down
2 changes: 1 addition & 1 deletion packages/theming/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

export { default as ThemeProvider } from './elements/ThemeProvider';
export { default as DEFAULT_THEME } from './elements/theme';
export type { IGardenTheme } from './elements/theme';
export { default as PALETTE } from './elements/palette';
export { default as isRtl } from './utils/isRtl';
export {
Expand All @@ -24,4 +25,3 @@ export type { ARROW_POSITION } from './utils/arrowStyles';
export { useDocument } from './utils/useDocument';
export { default as menuStyles } from './utils/menuStyles';
export type { MENU_POSITION } from './utils/menuStyles';
export type { DefaultTheme } from 'styled-components';
101 changes: 3 additions & 98 deletions utils/build/styled.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,104 +6,9 @@
*/

import 'styled-components';
import { IGardenTheme } from '../../packages/theming/src';

declare module 'styled-components' {
type Hue = Record<number | string, string> | string;

// eslint-disable-next-line @typescript-eslint/naming-convention
export interface DefaultTheme {
rtl: boolean;
document?: any;
borders: {
sm: string;
md: string;
};
borderRadii: {
sm: string;
md: string;
};
borderStyles: {
solid: string;
};
borderWidths: {
sm: string;
md: string;
};
breakpoints: {
xs: string;
sm: string;
md: string;
lg: string;
xl: string;
};
colors: {
base: 'light' | 'dark';
background: string;
foreground: string;
primaryHue: string;
dangerHue: string;
warningHue: string;
successHue: string;
neutralHue: string;
chromeHue: string;
};
components: Record<string, any>;
fonts: {
mono: string;
system: string;
};
fontSizes: {
xs: string;
sm: string;
md: string;
lg: string;
xl: string;
xxl: string;
xxxl: string;
};
fontWeights: {
thin: number;
extralight: number;
light: number;
regular: number;
medium: number;
semibold: number;
bold: number;
extrabold: number;
black: number;
};
iconSizes: {
sm: string;
md: string;
lg: string;
};
lineHeights: {
sm: string;
md: string;
lg: string;
xl: string;
xxl: string;
xxxl: string;
};
shadowWidths: {
sm: string;
md: string;
};
shadows: {
sm: (color: string) => string;
md: (color: string) => string;
lg: (offsetY: string, blurRadius: string, color: string) => string;
};
space: {
base: number;
xxs: string;
xs: string;
sm: string;
md: string;
lg: string;
xl: string;
xxl: string;
};
palette: Record<string, Hue>;
}
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
export interface DefaultTheme extends IGardenTheme {}
}