-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@suid/base": patch | ||
"@suid/material": patch | ||
--- | ||
|
||
Extract `createComponentFactory` functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import useTheme from "./useTheme"; | ||
import { PropsOf, InPropsOf, DefaultPropsOf } from "@suid/types"; | ||
import { mergeProps } from "solid-js"; | ||
|
||
export type ThemePropOptions<C> = { | ||
name: string; | ||
props: PropsOf<C>; | ||
propDefaults?: DefaultPropsOf<C> | PropDefaultsCb<C>; | ||
}; | ||
|
||
export type PropDefaultsCb<C> = (data: { | ||
set: (props: Omit<DefaultPropsOf<C>, "children">) => InPropsOf<C>; | ||
theme: ReturnType<typeof useTheme>; | ||
inProps: PropsOf<C>; | ||
}) => InPropsOf<C>; | ||
|
||
export default function useThemeProps<C>(options: ThemePropOptions<C>) { | ||
const theme = useTheme(); | ||
const set = (v: any) => v; | ||
const propDefaults = | ||
typeof options.propDefaults === "function" | ||
? (options.propDefaults as PropDefaultsCb<C>)({ | ||
set, | ||
theme, | ||
inProps: options.props, | ||
}) | ||
: options.propDefaults; | ||
return mergeProps( | ||
...[ | ||
...(propDefaults ? [propDefaults] : []), | ||
() => theme.components?.[options.name]?.defaultProps || {}, | ||
options.props, | ||
] | ||
) as InPropsOf<C>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,22 @@ | ||
import * as ComponentTypes from "./components-types"; | ||
import useTheme from "./useTheme"; | ||
import { PropsOf, InPropsOf } from "@suid/types"; | ||
import { mergeProps } from "solid-js"; | ||
import useBaseThemeProps, { | ||
ThemePropOptions as BaseThemePropOptions, | ||
} from "@suid/base/useThemeProps"; | ||
|
||
export type UseThemePropsName = keyof typeof ComponentTypes; | ||
export type UseThemePropsComponent<N extends UseThemePropsName> = | ||
export type ThemePropsName = keyof typeof ComponentTypes; | ||
export type ThemePropsComponent<N extends ThemePropsName> = | ||
typeof ComponentTypes[N]; | ||
export type UseThemePropsOptions< | ||
N extends UseThemePropsName, | ||
C = UseThemePropsComponent<N> | ||
> = { | ||
|
||
export type ThemePropOptions< | ||
N extends ThemePropsName, | ||
C = ThemePropsComponent<N> | ||
> = Omit<BaseThemePropOptions<C>, "name"> & { | ||
name: N; | ||
props: PropsOf<C>; | ||
propDefaults?: (data: { | ||
set: (props: InPropsOf<C>) => InPropsOf<C>; | ||
inProps: PropsOf<C>; | ||
}) => InPropsOf<C>; | ||
}; | ||
|
||
export default function useThemeProps< | ||
N extends UseThemePropsName, | ||
C = UseThemePropsComponent<N> | ||
>(options: UseThemePropsOptions<N, C>) { | ||
const theme = useTheme(); | ||
const set = (v: any) => v; | ||
const inProps = mergeProps( | ||
() => theme.components?.[options.name]?.defaultProps ?? {}, | ||
() => | ||
options.propDefaults?.({ | ||
set, | ||
inProps: options.props ?? (inProps as any), | ||
}) ?? {}, | ||
options.props | ||
) as InPropsOf<C>; | ||
return inProps; | ||
N extends ThemePropsName, | ||
C = ThemePropsComponent<N> | ||
>(options: ThemePropOptions<N, C>) { | ||
return useBaseThemeProps<C>(options); | ||
} |