Skip to content

Commit

Permalink
Merge 1d8114f into f849d97
Browse files Browse the repository at this point in the history
  • Loading branch information
heyjul3s committed Dec 8, 2020
2 parents f849d97 + 1d8114f commit 4488554
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/block/src/createBlockComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createBaseComponents } from '@artifak/component-generator';
import { BlockBaseProps } from './typings';
import { BlockBase } from './BlockBase';

/* eslint-disable @typescript-eslint/no-explicit-any */
export function createBlockComponents<S>(
styles: S
): { [key in keyof any]: React.ComponentType<BlockBaseProps> } {
Expand Down
48 changes: 27 additions & 21 deletions packages/component-generator/src/createBaseComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import React, { HTMLAttributes } from 'react';
import { AnyStyledComponent } from 'styled-components';
import { ComponentVariant, Settings, StyledSystemCSSObject } from './typings';
import { Variant, Settings, StyledSystemCSSObject } from './typings';

export function createBaseComponents<
S,
P = Record<string, unknown>,
E = HTMLDivElement
>(
BaseStyledComponent: AnyStyledComponent,
Base: AnyStyledComponent,
settings:
| {
[key in keyof S]: Settings<HTMLAttributes<E>>;
}
| {
[key in keyof S]: StyledSystemCSSObject;
}
): { [key in keyof S]: React.ComponentType<P & ComponentVariant> } {
const acc = {} as {
[key in keyof S]: React.ComponentType<P & ComponentVariant>;
};
| { [key in keyof S]: Settings<HTMLAttributes<E>> }
| { [key in keyof S]: StyledSystemCSSObject }
): { [key in keyof S]: React.ComponentType<P & Variant> } {
const acc = {} as { [key in keyof S]: React.ComponentType<P & Variant> };

return !!settings && Object.keys(settings).length >= 1
? Object.entries(settings).reduce((acc, entry) => {
const [prop, setting] = entry;
const entrySetting = extractSettings<E>(setting);
const entrySetting = extractSettings<E>(
setting as Settings<HTMLAttributes<E>> | StyledSystemCSSObject
);

if (hasKey(settings, prop)) {
acc[prop] = createStyledFunctionComponent<E, P>(
BaseStyledComponent,
entrySetting
);
acc[prop] = createStyledFunctionComponent<E, P>(Base, entrySetting);

acc[prop].displayName = prop;
}
Expand All @@ -39,6 +32,19 @@ export function createBaseComponents<
: acc;
}

export function extractSettings<E>(
setting: Settings<HTMLAttributes<E>>
): StyledSystemCSSObject & HTMLAttributes<E>;

export function extractSettings<E>(
setting: StyledSystemCSSObject
): StyledSystemCSSObject & HTMLAttributes<E>;

export function extractSettings<E>(
setting: Settings<HTMLAttributes<E>> | StyledSystemCSSObject
): StyledSystemCSSObject & HTMLAttributes<E>;

/* eslint-disable @typescript-eslint/no-explicit-any */
export function extractSettings<E>(
setting: any
): StyledSystemCSSObject & HTMLAttributes<E> {
Expand All @@ -48,13 +54,13 @@ export function extractSettings<E>(
}

export function createStyledFunctionComponent<E, P>(
BaseStyledComponent: AnyStyledComponent,
Base: AnyStyledComponent,
setting: StyledSystemCSSObject & HTMLAttributes<E>
): React.ComponentType<P> {
): React.FC<P> {
return props => (
<BaseStyledComponent {...setting} {...props}>
<Base {...setting} {...props}>
{props.children}
</BaseStyledComponent>
</Base>
);
}

Expand Down
7 changes: 6 additions & 1 deletion packages/component-generator/src/createStyledComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export function applyStyledComponent<
styles = {},
variants,
styleProps = []
}: StyledComponentConfig<Props, Theme, Attributes>) {
}: StyledComponentConfig<Props, Theme, Attributes>): StyledComponent<
keyof JSX.IntrinsicElements | React.ComponentType<Props>,
Theme | any,
React.ComponentType<Props & BaseComponentProps>,
keyof any
> {
return component(
styles,
variant({ variants }),
Expand Down
6 changes: 1 addition & 5 deletions packages/component-generator/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ export {

export { createStyledComponent } from './createStyledComponent';

export {
BaseComponentProps,
ComponentVariant,
StyledComponentConfig
} from './typings';
export { BaseComponentProps, Variant, StyledComponentConfig } from './typings';
2 changes: 1 addition & 1 deletion packages/component-generator/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type BaseComponentProps =
| SpaceProps
| TypographyProps;

export type ComponentVariant<ThemeType = void> = {
export type Variant<ThemeType = void> = {
variant?: ResponsiveValue<string | number, ThemeType>;
};

Expand Down

0 comments on commit 4488554

Please sign in to comment.