Skip to content

Commit

Permalink
feat(Theme): improve token typing
Browse files Browse the repository at this point in the history
  • Loading branch information
pylafleur committed Dec 10, 2024
1 parent ecbaaed commit f844301
Show file tree
Hide file tree
Showing 93 changed files with 644 additions and 1,455 deletions.
4 changes: 2 additions & 2 deletions packages/react/src/components/accordion/accordion-item.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import styled from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { Button } from '../buttons/button';
import { ResolvedTheme } from '../../themes';
import { Button } from '../buttons';
import { Icon } from '../icon/icon';
import { Heading, Type, Tag } from '../heading/heading';
import { focus } from '../../utils/css-state';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/avatar/avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo, VoidFunctionComponent } from 'react';
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { getInitialsFromUsername } from '../../utils/user';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { Icon } from '../icon/icon';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/buttons/abstract/styled.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { ResolvedTheme } from '../../../themes/theme';
import { ResolvedTheme } from '../../../themes';
import { focus } from '../../../utils/css-state';
import { Size } from './types';

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/buttons/icon-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { forwardRef, ReactElement, Ref } from 'react';
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { AvatarProps } from '../avatar/avatar';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { Icon, IconName, IconProps } from '../icon/icon';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/buttons/styled.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { Icon } from '../icon/icon';
import { Spinner } from '../spinner/spinner';
import { AbstractButton } from './abstract/abstract-button';
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
import styled from 'styled-components';
import { useDataAttributes } from '../../hooks/use-data-attributes';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { FieldContainer } from '../field-container/field-container';
import { IconButton } from '../buttons/icon-button';
import { IconButton } from '../buttons';
import { Listbox, ListboxOption } from '../listbox/listbox';
import { TooltipProps } from '../tooltip/tooltip';
import { useAriaConditionalIds } from '../../hooks/use-aria-conditional-ids';
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/date-picker/date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import { parse } from 'date-fns';
import datepickerCss from 'react-datepicker/dist/react-datepicker.min.css';
import styled, { createGlobalStyle } from 'styled-components';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { eventIsInside } from '../../utils/events';
import { v4 as uuid } from '../../utils/uuid';
import { AbstractButton } from '../buttons/abstract/abstract-button';
import { Button } from '../buttons/button';
import { AbstractButton, Button } from '../buttons';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { FieldContainer } from '../field-container/field-container';
import { Icon } from '../icon/icon';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import styled from 'styled-components';
import { useDataAttributes } from '../../hooks/use-data-attributes';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { isLetterOrNumber } from '../../utils/regex';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FunctionComponent, PropsWithChildren } from 'react';
import styled from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { InvalidField } from '../feedbacks/invalid-field';
import { Label } from '../label/label';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/link/styled.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { getButtonStyles } from '../buttons';
import { LinkProps } from './types';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/lozenge/lozenge.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FunctionComponent, PropsWithChildren } from 'react';
import styled from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
import { Icon, IconName } from '../icon/icon';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';

interface DescriptionProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/search/search-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XIcon from 'feather-icons/dist/icons/x.svg';
import { ChangeEvent, FocusEvent, KeyboardEvent, useCallback, useMemo, useRef, VoidFunctionComponent } from 'react';
import styled from 'styled-components';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { v4 as uuid } from '../../utils/uuid';
import { SearchButton } from '../buttons';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import styled, { css, ThemedCssFunction } from 'styled-components';
import { useId } from '../../hooks/use-id';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { Button, IconButton } from '../buttons';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/status/status.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VoidFunctionComponent } from 'react';
import styled from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';

export type StatusType = 'enabled' | 'disabled' | 'blocked';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import React, {
import styled from 'styled-components';
import { useId } from '../../hooks/use-id';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { DeviceContextProps, useDeviceContext } from '../device-context-provider/device-context-provider';
import { FieldContainer } from '../field-container/field-container';
import { responsiveInputsStyle } from '../text-input/styles/inputs';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/table/table-row.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CSSProperties, ReactElement } from 'react';
import { flexRender, Row } from '@tanstack/react-table';
import styled, { css, FlattenInterpolation, ThemedStyledProps, ThemeProps } from 'styled-components';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { isLastColumnInAGroup } from './utils/table-utils';
import { CustomCell } from './types';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/text-area/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import styled from 'styled-components';
import { useDataAttributes } from '../../hooks/use-data-attributes';
import { useId } from '../../hooks/use-id';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { FieldContainer } from '../field-container/field-container';
import { inputsStyle } from '../text-input/styles/inputs';
import { TooltipProps } from '../tooltip/tooltip';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/text-input/styles/inputs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { css, FlattenSimpleInterpolation } from 'styled-components';
import { ResolvedTheme } from '../../../themes/theme';
import { ResolvedTheme } from '../../../themes';
import { focus } from '../../../utils/css-state';
import { DeviceContextProps } from '../../device-context-provider/device-context-provider';

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/toast/toast-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import styled, {
} from 'styled-components';
import { useToasts } from '../../hooks/use-toasts';
import { useTranslation } from '../../i18n/use-translation';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { IconButton, IconButtonProps } from '../buttons/icon-button';
import { DeviceContextProps, useDeviceContext } from '../device-context-provider/device-context-provider';
import { Icon, IconName, IconProps } from '../icon/icon';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/toast/toasts-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
import { useShadowRoot } from 'react-shadow';
import styled, { css, FlattenInterpolation, ThemeProps } from 'styled-components';
import { useToasts } from '../../hooks/use-toasts';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { DeviceContextProps, useDeviceContext } from '../device-context-provider/device-context-provider';
import { ToastContainer } from './toast-container';
import { ToastPosition } from './toast-position';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo, VoidFunctionComponent } from 'react';
import styled from 'styled-components';
import { useDataAttributes } from '../../hooks/use-data-attributes';
import { ResolvedTheme } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { v4 as uuid } from '../../utils/uuid';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/components/tooltip/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { PopperOptions, TriggerType, usePopperTooltip } from 'react-popper-tooltip';
import styled, { css } from 'styled-components';
import { useTheme } from '../../hooks/use-theme';
import { ResolvedTheme as ThemeType } from '../../themes/theme';
import { ResolvedTheme } from '../../themes';
import { focus } from '../../utils/css-state';
import { v4 as uuid } from '../../utils/uuid';
import { useDeviceContext } from '../device-context-provider/device-context-provider';
Expand Down Expand Up @@ -53,7 +53,7 @@ interface TooltipContainerProps {
}

interface TooltipColorProps {
theme: ThemeType;
theme: ResolvedTheme;
variant: TooltipVariant;
}

Expand All @@ -64,7 +64,7 @@ const tooltipColor = css<TooltipColorProps>`${({ theme, variant }) => {
return theme.component['tooltip-popper-container-default-background-color'];
}}`;

const tooltipBorderColor = css<{ theme: ThemeType }>`${({ theme }) => theme.component['tooltip-popper-container-border-color']}`;
const tooltipBorderColor = css<{ theme: ResolvedTheme }>`${({ theme }) => theme.component['tooltip-popper-container-border-color']}`;

const TooltipContainer = styled.div<TooltipContainerProps>`
background-color: ${tooltipColor};
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from 'react';
import { ThemeContext } from 'styled-components';
import { ResolvedTheme } from '../themes/theme';
import { ResolvedTheme } from '../themes';

export function useTheme(): ResolvedTheme {
return useContext(ThemeContext);
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/styled-components-theme.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResolvedTheme } from './themes/theme';
import { ResolvedTheme } from './themes';

declare module 'styled-components' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
38 changes: 20 additions & 18 deletions packages/react/src/themes/build-theme.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { devConsole } from '../utils/dev-console';
import { ResolvedTheme, ThemeCustomization } from './theme';
import { ResolvedTheme, ThemeCustomization, ThemeDeclaration } from './theme';
import {
AliasTokenMap,
AliasTokens,
ComponentTokenMap,
AliasToken,
ComponentToken,
defaultAliasTokens,
defaultComponentTokens,
defaultRefTokens,
isAliasToken,
isComponentToken,
isRefToken,
RefTokenMap,
RefTokens,
RefTokenValue,
ResolvedComponentTokens,
RefToken,
ResolvedTokenMap,
TokenValue,
TokenName,
} from './tokens';

interface CustomizedTheme {
ref: RefTokenMap;
alias: AliasTokenMap;
component: ComponentTokenMap;
function isRefToken(tokenName: TokenName): tokenName is RefToken {
return tokenName in defaultRefTokens;
}

function customizeTheme(customization: ThemeCustomization): CustomizedTheme {
function isAliasToken(tokenName: TokenName): tokenName is AliasToken {
return tokenName in defaultAliasTokens;
}

function isComponentToken(tokenName: TokenName): tokenName is ComponentToken {
return tokenName in defaultComponentTokens;
}

function customizeTheme(customization: ThemeCustomization): ThemeDeclaration {
return {
ref: { ...defaultRefTokens, ...customization.ref },
alias: { ...defaultAliasTokens, ...customization.alias },
Expand All @@ -35,10 +37,10 @@ export function buildTheme(customization: ThemeCustomization): ResolvedTheme {

const resolvedTheme: ResolvedTheme = {
...customizedTheme,
component: {} as ResolvedComponentTokens,
component: {} as ResolvedTokenMap<ComponentToken>,
};

function resolveToken(token: AliasTokens | RefTokens): RefTokenValue {
function resolveToken(token: AliasToken | RefToken): TokenValue {
if (isRefToken(token)) {
return resolvedTheme.ref[token];
}
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/themes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './equisoft';
export * from './tokens';
export * from './build-theme';
export * from './theme';
30 changes: 22 additions & 8 deletions packages/react/src/themes/theme.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { AliasTokenMap, ComponentTokenMap, RefTokenMap, ResolvedAliasTokens, ResolvedComponentTokens } from './tokens';
import type {
AliasToken,
AliasTokenMap,
ComponentToken,
ComponentTokenMap,
RefToken,
RefTokenMap,
ResolvedTokenMap,
} from './tokens';

export interface ThemeCustomization {
ref?: Partial<RefTokenMap>;
alias?: Partial<AliasTokenMap>;
component?: Partial<ComponentTokenMap>;
export interface ThemeDeclaration {
ref: RefTokenMap<RefToken>;
alias: AliasTokenMap<AliasToken>;
component: ComponentTokenMap<ComponentToken>;
}

export type ThemeCustomization = {
ref?: Partial<RefTokenMap<RefToken>>;
alias?: Partial<AliasTokenMap<AliasToken>>;
component?: Partial<ComponentTokenMap<ComponentToken>>;
}

export interface ResolvedTheme {
ref: RefTokenMap;
alias: ResolvedAliasTokens;
component: ResolvedComponentTokens;
ref: ResolvedTokenMap<RefToken>;
alias: ResolvedTokenMap<AliasToken>;
component: ResolvedTokenMap<ComponentToken>;
}
Loading

0 comments on commit f844301

Please sign in to comment.