mirror of
https://github.com/zoriya/yoshiki.git
synced 2025-12-06 07:06:13 +00:00
Cleanup types
This commit is contained in:
@@ -3,11 +3,11 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for details.
|
||||
//
|
||||
|
||||
import { FilterOr, WithState, YoshikiStyle, Length, StyleList } from "../type";
|
||||
import { WithState, YoshikiStyle, StyleList } from "../type";
|
||||
import { shorthandsFn } from "../shorthands";
|
||||
import { ImageStyle, StyleProp, TextStyle, ViewStyle } from "react-native";
|
||||
import { Theme } from "../theme";
|
||||
import { forceBreakpoint, WithBreakpoints } from "../utils";
|
||||
import { forceBreakpoint } from "../utils";
|
||||
|
||||
// The extends any check is only used to make EnhancedStyle a distributive type.
|
||||
// This means EnhancedStyle<ViewStyle | TextStyle> = EnhancedStyle<ViewStyle> | EnhancedStyle<TextStyle>
|
||||
@@ -15,11 +15,7 @@ export type EnhancedStyle<Properties> = Properties extends any
|
||||
? {
|
||||
[key in keyof Properties]: YoshikiStyle<Properties[key]>;
|
||||
} & {
|
||||
[key in keyof typeof shorthandsFn]?: FilterOr<
|
||||
Parameters<typeof shorthandsFn[key]>[0],
|
||||
Length,
|
||||
YoshikiStyle<number>
|
||||
>;
|
||||
[key in keyof typeof shorthandsFn]?: Parameters<typeof shorthandsFn[key]>[0];
|
||||
}
|
||||
: never;
|
||||
|
||||
|
||||
@@ -3,51 +3,46 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for details.
|
||||
//
|
||||
|
||||
import type { CommonStyle } from "./type";
|
||||
import type { ViewStyle } from "react-native";
|
||||
|
||||
type YSPs = CommonStyle<ViewStyle>;
|
||||
|
||||
export const shorthandsFn = {
|
||||
p: (v: YSPs["padding"]): YSPs => ({
|
||||
p: (v: string | number) => ({
|
||||
padding: v,
|
||||
}),
|
||||
pX: (v: YSPs["paddingLeft"]): YSPs => ({
|
||||
pX: (v: string | number) => ({
|
||||
paddingLeft: v,
|
||||
paddingRight: v,
|
||||
}),
|
||||
paddingX: (v: YSPs["paddingLeft"]): YSPs => ({
|
||||
paddingX: (v: string | number) => ({
|
||||
paddingLeft: v,
|
||||
paddingRight: v,
|
||||
}),
|
||||
pY: (v: YSPs["paddingTop"]): YSPs => ({
|
||||
pY: (v: string | number) => ({
|
||||
paddingTop: v,
|
||||
paddingBottom: v,
|
||||
}),
|
||||
paddingY: (v: YSPs["paddingTop"]): YSPs => ({
|
||||
paddingY: (v: string | number) => ({
|
||||
paddingTop: v,
|
||||
paddingBottom: v,
|
||||
}),
|
||||
m: (v: YSPs["margin"]): YSPs => ({
|
||||
m: (v: string | number) => ({
|
||||
margin: v,
|
||||
}),
|
||||
mX: (v: YSPs["marginLeft"]): YSPs => ({
|
||||
mX: (v: string | number) => ({
|
||||
marginLeft: v,
|
||||
marginRight: v,
|
||||
}),
|
||||
marginX: (v: YSPs["marginLeft"]): YSPs => ({
|
||||
marginX: (v: string | number) => ({
|
||||
marginLeft: v,
|
||||
marginRight: v,
|
||||
}),
|
||||
mY: (v: YSPs["marginTop"]): YSPs => ({
|
||||
mY: (v: string | number) => ({
|
||||
marginTop: v,
|
||||
marginBottom: v,
|
||||
}),
|
||||
marginY: (v: YSPs["marginTop"]): YSPs => ({
|
||||
marginY: (v: string | number) => ({
|
||||
marginTop: v,
|
||||
marginBottom: v,
|
||||
}),
|
||||
bg: (v: YSPs["backgroundColor"]): YSPs => ({
|
||||
bg: (v: string) => ({
|
||||
backgroundColor: v,
|
||||
}),
|
||||
};
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
//
|
||||
|
||||
import { breakpoints, Theme } from "./theme";
|
||||
import { Properties as _CssProperties } from "csstype";
|
||||
import { shorthandsFn } from "./shorthands";
|
||||
|
||||
export type YoshikiStyle<Property> =
|
||||
| Property
|
||||
@@ -35,25 +33,3 @@ export const processStyleList = <Style>(los: StyleList<Style>): Partial<Style> =
|
||||
if (isReadonlyArray(los)) return los.reduce((acc, x) => ({ ...acc, ...processStyleList(x) }), {});
|
||||
return los ? los : {};
|
||||
};
|
||||
|
||||
// dummy type only used for the API.
|
||||
export type Length = { a: 1 };
|
||||
export type CssProperties = _CssProperties<0 | Length | string>;
|
||||
|
||||
type FilterOrNever<T, Filter> = T extends Filter ? Filter : never;
|
||||
export type FilterOr<T, Filter, Replacement = Filter> = [FilterOrNever<T, Filter>] extends [never]
|
||||
? T
|
||||
: Replacement;
|
||||
|
||||
type CombineWithLength<A, B> = {
|
||||
[key in keyof A & keyof B]?: FilterOr<(A & B)[key], Length>;
|
||||
};
|
||||
export type CommonCss<Style> = CombineWithLength<CssProperties, Style>;
|
||||
|
||||
export type CommonStyle<Style> = {
|
||||
[key in keyof CommonCss<Style>]: YoshikiStyle<CommonCss<Style>[key]>;
|
||||
};
|
||||
|
||||
export type EnhancedStyle<Style> = CommonStyle<Style> & {
|
||||
[key in keyof typeof shorthandsFn]?: Parameters<typeof shorthandsFn[key]>[0];
|
||||
};
|
||||
|
||||
@@ -5,8 +5,9 @@
|
||||
|
||||
import { useInsertionEffect } from "react";
|
||||
import { prefix } from "inline-style-prefixer";
|
||||
import { Properties as CssProperties } from "csstype";
|
||||
import { Theme, breakpoints, useTheme } from "../theme";
|
||||
import { WithState, YoshikiStyle, CssProperties, StyleList, processStyleList } from "../type";
|
||||
import { WithState, YoshikiStyle, StyleList, processStyleList } from "../type";
|
||||
import { forceBreakpoint, isBreakpoints } from "../utils";
|
||||
import { StyleRegistry, useStyleRegistry } from "./registry";
|
||||
import { shorthandsFn } from "../shorthands";
|
||||
|
||||
Reference in New Issue
Block a user