rn-css 1.8.0 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/convertStyle.d.ts +2 -3
- package/dist/convertUnits.js +1 -1
- package/dist/cssToRN/index.d.ts +1 -1
- package/dist/index.d.ts +157 -156
- package/dist/rnToCss.d.ts +2 -2
- package/dist/styleComponent.d.ts +14 -14
- package/dist/types.d.ts +4 -2
- package/package.json +1 -1
- package/src/convertStyle.tsx +5 -5
- package/src/convertUnits.ts +1 -1
- package/src/index.tsx +2 -1
- package/src/rnToCss.ts +2 -2
- package/src/styleComponent.tsx +17 -17
- package/src/types.ts +6 -2
package/dist/rnToCss.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
declare const rnToCSS: (rn: Partial<
|
|
1
|
+
import { CompleteStyle } from './types';
|
|
2
|
+
declare const rnToCSS: (rn: Partial<CompleteStyle>) => string;
|
|
3
3
|
export default rnToCSS;
|
package/dist/styleComponent.d.ts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
1
|
import React, { MouseEvent } from 'react';
|
|
2
|
-
import { FlatListProps, LayoutChangeEvent, SectionListProps, StyleProp,
|
|
3
|
-
import type { Units } from './types';
|
|
2
|
+
import { FlatListProps, LayoutChangeEvent, SectionListProps, StyleProp, VirtualizedListProps } from 'react-native';
|
|
3
|
+
import type { AnyStyle, CompleteStyle, Units } from './types';
|
|
4
4
|
export declare const defaultUnits: Units;
|
|
5
5
|
export declare const RemContext: React.Context<number>;
|
|
6
6
|
export declare const FontSizeContext: React.Context<number>;
|
|
7
7
|
export declare const SharedValue: React.Context<unknown>;
|
|
8
8
|
export interface DefaultTheme {
|
|
9
9
|
}
|
|
10
|
-
declare type Primitive = number | string | null | undefined | boolean |
|
|
10
|
+
declare type Primitive = number | string | null | undefined | boolean | CompleteStyle;
|
|
11
11
|
declare type Functs<T> = (arg: T & {
|
|
12
12
|
rnCSS?: string;
|
|
13
13
|
shared: unknown;
|
|
14
14
|
theme: DefaultTheme;
|
|
15
15
|
}) => Primitive;
|
|
16
|
-
declare type OptionalProps = {
|
|
16
|
+
declare type OptionalProps<T extends AnyStyle = AnyStyle> = {
|
|
17
17
|
rnCSS?: `${string};`;
|
|
18
18
|
onMouseEnter?: (event: MouseEvent) => void;
|
|
19
19
|
onMouseLeave?: (event: MouseEvent) => void;
|
|
20
20
|
onLayout?: (event: LayoutChangeEvent) => void;
|
|
21
21
|
children?: React.ReactNode;
|
|
22
|
-
style?: StyleProp<
|
|
22
|
+
style?: StyleProp<T>;
|
|
23
23
|
};
|
|
24
|
-
declare const styled: <Props>(Component: React.ComponentType<Props>) => {
|
|
25
|
-
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]): React.ForwardRefExoticComponent<Props & S & OptionalProps & {
|
|
24
|
+
declare const styled: <Props, StyleType extends AnyStyle = AnyStyle>(Component: React.ComponentType<Props>) => {
|
|
25
|
+
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]): React.ForwardRefExoticComponent<Props & S & OptionalProps<StyleType> & {
|
|
26
26
|
ref?: React.Ref<any> | undefined;
|
|
27
27
|
}>;
|
|
28
|
-
attrs<S_1>(opts: Partial<S_1 & Props> | ((props: S_1 & Props) => Partial<S_1 & Props>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & Props>)[]) => React.ForwardRefExoticComponent<(Props | S_1) & OptionalProps & {
|
|
28
|
+
attrs<S_1>(opts: Partial<S_1 & Props> | ((props: S_1 & Props) => Partial<S_1 & Props>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & Props>)[]) => React.ForwardRefExoticComponent<(Props | S_1) & OptionalProps<StyleType> & {
|
|
29
29
|
ref?: React.Ref<any> | undefined;
|
|
30
30
|
}>;
|
|
31
31
|
};
|
|
32
32
|
export default styled;
|
|
33
33
|
export declare const styledFlatList: {
|
|
34
|
-
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S>)[]): <Type>(props: S & FlatListProps<Type> & OptionalProps) => JSX.Element;
|
|
35
|
-
attrs<S_1>(opts: Partial<S_1 & FlatListProps<any>> | ((props: S_1 & FlatListProps<any>) => Partial<S_1 & FlatListProps<any>>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & FlatListProps<any>>)[]) => <Props>(componentProps: S_1 & FlatListProps<Props> & OptionalProps) => JSX.Element;
|
|
34
|
+
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S>)[]): <Type>(props: S & FlatListProps<Type> & OptionalProps<AnyStyle>) => JSX.Element;
|
|
35
|
+
attrs<S_1>(opts: Partial<S_1 & FlatListProps<any>> | ((props: S_1 & FlatListProps<any>) => Partial<S_1 & FlatListProps<any>>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & FlatListProps<any>>)[]) => <Props>(componentProps: S_1 & FlatListProps<Props> & OptionalProps<AnyStyle>) => JSX.Element;
|
|
36
36
|
};
|
|
37
37
|
export declare const styledSectionList: {
|
|
38
|
-
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S>)[]): <Type>(props: S & SectionListProps<Type, import("react-native").DefaultSectionT> & OptionalProps) => JSX.Element;
|
|
39
|
-
attrs<S_1>(opts: Partial<S_1 & SectionListProps<any, import("react-native").DefaultSectionT>> | ((props: S_1 & SectionListProps<any, import("react-native").DefaultSectionT>) => Partial<S_1 & SectionListProps<any, import("react-native").DefaultSectionT>>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & SectionListProps<any, import("react-native").DefaultSectionT>>)[]) => <Props>(componentProps: S_1 & SectionListProps<Props, import("react-native").DefaultSectionT> & OptionalProps) => JSX.Element;
|
|
38
|
+
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S>)[]): <Type>(props: S & SectionListProps<Type, import("react-native").DefaultSectionT> & OptionalProps<AnyStyle>) => JSX.Element;
|
|
39
|
+
attrs<S_1>(opts: Partial<S_1 & SectionListProps<any, import("react-native").DefaultSectionT>> | ((props: S_1 & SectionListProps<any, import("react-native").DefaultSectionT>) => Partial<S_1 & SectionListProps<any, import("react-native").DefaultSectionT>>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & SectionListProps<any, import("react-native").DefaultSectionT>>)[]) => <Props>(componentProps: S_1 & SectionListProps<Props, import("react-native").DefaultSectionT> & OptionalProps<AnyStyle>) => JSX.Element;
|
|
40
40
|
};
|
|
41
41
|
export declare const styledVirtualizedList: {
|
|
42
|
-
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S>)[]): <Type>(props: S & VirtualizedListProps<Type> & OptionalProps) => JSX.Element;
|
|
43
|
-
attrs<S_1>(opts: Partial<S_1 & VirtualizedListProps<any>> | ((props: S_1 & VirtualizedListProps<any>) => Partial<S_1 & VirtualizedListProps<any>>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & VirtualizedListProps<any>>)[]) => <Props>(componentProps: S_1 & VirtualizedListProps<Props> & OptionalProps) => JSX.Element;
|
|
42
|
+
<S>(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S>)[]): <Type>(props: S & VirtualizedListProps<Type> & OptionalProps<AnyStyle>) => JSX.Element;
|
|
43
|
+
attrs<S_1>(opts: Partial<S_1 & VirtualizedListProps<any>> | ((props: S_1 & VirtualizedListProps<any>) => Partial<S_1 & VirtualizedListProps<any>>)): (chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S_1 & VirtualizedListProps<any>>)[]) => <Props>(componentProps: S_1 & VirtualizedListProps<Props> & OptionalProps<AnyStyle>) => JSX.Element;
|
|
44
44
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ViewStyle, TextStyle } from 'react-native';
|
|
1
|
+
import { ViewStyle, TextStyle, ImageStyle } from 'react-native';
|
|
2
2
|
export declare type Units = {
|
|
3
3
|
'%'?: number;
|
|
4
4
|
vw?: number;
|
|
@@ -51,7 +51,9 @@ export declare type Context = {
|
|
|
51
51
|
deviceAspectRatio: number;
|
|
52
52
|
units: Units;
|
|
53
53
|
};
|
|
54
|
-
export declare type
|
|
54
|
+
export declare type AnyStyle = ViewStyle | TextStyle | ImageStyle;
|
|
55
|
+
export declare type CompleteStyle = ViewStyle & TextStyle & ImageStyle;
|
|
56
|
+
export declare type PartialStyle = Partial<Record<keyof CompleteStyle, string>> & {
|
|
55
57
|
shadowOffset?: {
|
|
56
58
|
width: string;
|
|
57
59
|
height: string;
|
package/package.json
CHANGED
package/src/convertStyle.tsx
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/* eslint-disable react/display-name */
|
|
2
|
-
import { /* StyleSheet, */ TextStyle
|
|
2
|
+
import { /* StyleSheet, */ TextStyle } from 'react-native'
|
|
3
3
|
import { convertValue } from './convertUnits'
|
|
4
|
-
import type { PartialStyle, Units } from './types'
|
|
4
|
+
import type { AnyStyle, CompleteStyle, PartialStyle, Units } from './types'
|
|
5
5
|
|
|
6
6
|
/** Mix the calculated RN style within the object style */
|
|
7
|
-
const convertStyle = (rnStyle: PartialStyle, units: Units) => {
|
|
7
|
+
const convertStyle = <T extends AnyStyle = AnyStyle>(rnStyle: PartialStyle, units: Units) => {
|
|
8
8
|
/** This is the result of the convertions from css style into RN style */
|
|
9
|
-
const convertedStyle:
|
|
9
|
+
const convertedStyle: CompleteStyle = {};
|
|
10
10
|
// If width and height are specified, we can use those values for the first render
|
|
11
11
|
(['width', 'height'] as const).forEach(key => {
|
|
12
12
|
if (!units[key] && rnStyle[key]) {
|
|
@@ -52,7 +52,7 @@ const convertStyle = (rnStyle: PartialStyle, units: Units) => {
|
|
|
52
52
|
convertedStyle[key] = convertValue(key, value, units)
|
|
53
53
|
}
|
|
54
54
|
})
|
|
55
|
-
return convertedStyle
|
|
55
|
+
return convertedStyle as T
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export default convertStyle
|
package/src/convertUnits.ts
CHANGED
|
@@ -11,7 +11,7 @@ export function parseValue (value: string): [number, string | undefined] {
|
|
|
11
11
|
|
|
12
12
|
/** Convert a value using the provided unit transform table */
|
|
13
13
|
export function convertValue (key: keyof PartialStyle | keyof Transform, value: string, units: Units): string | number {
|
|
14
|
-
if (
|
|
14
|
+
if (typeof value !== 'string') {
|
|
15
15
|
console.error(`Failed to parse CSS instruction: ${key}=${value}. We expect a string, but ${value} was of type ${typeof value}.`)
|
|
16
16
|
return 0
|
|
17
17
|
}
|
package/src/index.tsx
CHANGED
|
@@ -2,11 +2,12 @@ import React from 'react'
|
|
|
2
2
|
import * as RN from 'react-native'
|
|
3
3
|
import './polyfill'
|
|
4
4
|
import styledComponent, { styledFlatList, styledSectionList, styledVirtualizedList } from './styleComponent'
|
|
5
|
+
import { AnyStyle } from './types'
|
|
5
6
|
export { cssToRNStyle } from './cssToRN'
|
|
6
7
|
export { SharedValue, FontSizeContext, RemContext, DefaultTheme } from './styleComponent'
|
|
7
8
|
export * from './useTheme'
|
|
8
9
|
|
|
9
|
-
const styled = <T, >(Component: React.ComponentType<T>) => styledComponent<T>(Component)
|
|
10
|
+
const styled = <T, >(Component: React.ComponentType<T>) => styledComponent<T, AnyStyle>(Component)
|
|
10
11
|
|
|
11
12
|
styled.ActivityIndicator = styled(RN.ActivityIndicator)
|
|
12
13
|
styled.Button = styled(RN.Button)
|
package/src/rnToCss.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CompleteStyle } from './types'
|
|
2
2
|
|
|
3
|
-
const rnToCSS = (rn: Partial<
|
|
3
|
+
const rnToCSS = (rn: Partial<CompleteStyle>) =>
|
|
4
4
|
Object.entries(rn)
|
|
5
5
|
.map(([key, value]) => `${camelToKebab(key)}: ${convertValue(value)};`)
|
|
6
6
|
.join('\n')
|
package/src/styleComponent.tsx
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/* eslint-disable react/prop-types */
|
|
2
2
|
/* eslint-disable react/display-name */
|
|
3
3
|
import React, { MouseEvent } from 'react'
|
|
4
|
-
import { FlatList, FlatListProps, LayoutChangeEvent, Platform, SectionList, SectionListProps, StyleProp, StyleSheet,
|
|
4
|
+
import { FlatList, FlatListProps, LayoutChangeEvent, Platform, SectionList, SectionListProps, StyleProp, StyleSheet, VirtualizedList, VirtualizedListProps } from 'react-native'
|
|
5
5
|
import convertStyle from './convertStyle'
|
|
6
6
|
import cssToStyle from './cssToRN'
|
|
7
7
|
import { useFontSize, useHover, useLayout, useScreenSize, useMediaQuery } from './features'
|
|
8
|
-
import type { Style, Units } from './types'
|
|
8
|
+
import type { AnyStyle, CompleteStyle, Style, Units } from './types'
|
|
9
9
|
import generateHash from './generateHash'
|
|
10
10
|
import rnToCSS from './rnToCss'
|
|
11
11
|
|
|
@@ -19,15 +19,15 @@ export const SharedValue = React.createContext<unknown>(undefined)
|
|
|
19
19
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
20
20
|
export interface DefaultTheme {}
|
|
21
21
|
|
|
22
|
-
type Primitive = number | string | null | undefined | boolean |
|
|
22
|
+
type Primitive = number | string | null | undefined | boolean | CompleteStyle
|
|
23
23
|
type Functs<T> = (arg: T & { rnCSS?: string; shared: unknown, theme: DefaultTheme }) => Primitive
|
|
24
|
-
type OptionalProps = {
|
|
24
|
+
type OptionalProps<T extends AnyStyle = AnyStyle> = {
|
|
25
25
|
rnCSS?: `${string};`;
|
|
26
26
|
onMouseEnter?: (event: MouseEvent) => void;
|
|
27
27
|
onMouseLeave?: (event: MouseEvent) => void;
|
|
28
28
|
onLayout?: (event: LayoutChangeEvent) => void
|
|
29
29
|
children?: React.ReactNode;
|
|
30
|
-
style?: StyleProp<
|
|
30
|
+
style?: StyleProp<T>;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/** Converts the tagged template string into a css string */
|
|
@@ -37,31 +37,31 @@ function buildCSSString<T extends { rnCSS?: string }> (chunks: TemplateStringsAr
|
|
|
37
37
|
.map((chunk, i) => ([chunk, (functs[i] instanceof Function) ? (functs[i] as Functs<T>)({ shared, theme: (shared as DefaultTheme), ...props }) : functs[i]]))
|
|
38
38
|
.flat()
|
|
39
39
|
// Convert the objects to string if the result is not a primitive
|
|
40
|
-
.map(chunk => typeof chunk === 'object' ? rnToCSS(chunk as Partial<
|
|
40
|
+
.map(chunk => typeof chunk === 'object' ? rnToCSS(chunk as Partial<CompleteStyle>) : chunk)
|
|
41
41
|
.join('')
|
|
42
42
|
if (props.rnCSS) computedString += props.rnCSS.replace(/=/gm, ':') + ';'
|
|
43
43
|
return computedString
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const styleMap: Record<string, { style:
|
|
47
|
-
function getStyle (hash: string, style:
|
|
46
|
+
const styleMap: Record<string, { style: AnyStyle, usage: number }> = {}
|
|
47
|
+
function getStyle <T extends AnyStyle, > (hash: string, style: T) {
|
|
48
48
|
const styleInfo = styleMap[hash]
|
|
49
49
|
if (styleInfo) {
|
|
50
50
|
styleInfo.usage++
|
|
51
|
-
return styleInfo.style
|
|
51
|
+
return styleInfo.style as T
|
|
52
52
|
}
|
|
53
53
|
else {
|
|
54
54
|
const sheet = StyleSheet.create({ [hash]: style })
|
|
55
|
-
return (styleMap[hash] = { style: sheet[hash], usage: 1 }).style
|
|
55
|
+
return (styleMap[hash] = { style: sheet[hash], usage: 1 }).style as T
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
function removeStyle (hash: string) {
|
|
59
59
|
styleMap[hash].usage--
|
|
60
60
|
if (styleMap[hash].usage <= 0) delete styleMap[hash]
|
|
61
61
|
}
|
|
62
|
-
const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
62
|
+
const styled = <Props, StyleType extends AnyStyle = AnyStyle>(Component: React.ComponentType<Props>) => {
|
|
63
63
|
const styledComponent = <S, >(chunks: TemplateStringsArray, ...functs: (Primitive | Functs<S & Props>)[]) => {
|
|
64
|
-
const ForwardRefComponent = React.forwardRef<React.ComponentType<S & Props & OptionalProps
|
|
64
|
+
const ForwardRefComponent = React.forwardRef<React.ComponentType<S & Props & OptionalProps<StyleType>>, S & Props & OptionalProps<StyleType>>((props: S & Props & OptionalProps<StyleType>, ref) => {
|
|
65
65
|
const rem = React.useContext(RemContext)
|
|
66
66
|
const shared = React.useContext(SharedValue)
|
|
67
67
|
// Build the css string with the context
|
|
@@ -120,13 +120,13 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
120
120
|
const units = React.useMemo<Units>(() => ({ ...baseUnits, em }), [baseUnits, em])
|
|
121
121
|
|
|
122
122
|
const { style: styleConvertedFromCSS, hash } = React.useMemo(() => {
|
|
123
|
-
const style = convertStyle(finalStyle, units)
|
|
123
|
+
const style = convertStyle<StyleType>(finalStyle, units)
|
|
124
124
|
delete (style as Style).textOverflow
|
|
125
125
|
const hash = generateHash(JSON.stringify(style))
|
|
126
|
-
return { style: getStyle(hash, style), hash }
|
|
126
|
+
return { style: getStyle<StyleType>(hash, style), hash }
|
|
127
127
|
}, [finalStyle, units])
|
|
128
128
|
const newProps = React.useMemo(() => {
|
|
129
|
-
const newProps: OptionalProps = { style: [styleConvertedFromCSS, props.style], onMouseEnter, onMouseLeave, onLayout }
|
|
129
|
+
const newProps: OptionalProps<StyleType> = { style: [styleConvertedFromCSS, props.style], onMouseEnter, onMouseLeave, onLayout }
|
|
130
130
|
if (finalStyle.textOverflow === 'ellipsis') {
|
|
131
131
|
Object.assign(newProps, { numberOfLines: 1 })
|
|
132
132
|
}
|
|
@@ -145,7 +145,7 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
145
145
|
return <Component ref={ref} {...props} {...newProps} />
|
|
146
146
|
}
|
|
147
147
|
})
|
|
148
|
-
return ForwardRefComponent as React.ForwardRefExoticComponent<Props & S & OptionalProps & { ref?: React.Ref<any> }>
|
|
148
|
+
return ForwardRefComponent as React.ForwardRefExoticComponent<Props & S & OptionalProps<StyleType> & { ref?: React.Ref<any> }>
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
// provide styled(Comp).attrs({} | () => {}) feature
|
|
@@ -156,7 +156,7 @@ const styled = <Props, >(Component: React.ComponentType<Props>) => {
|
|
|
156
156
|
return <ComponentWithAttrs ref={ref} {...props} {...attrs} />
|
|
157
157
|
})
|
|
158
158
|
// TODO : Find a way to remove from the Props the properties affected by opts
|
|
159
|
-
return ForwardRefComponent as React.ForwardRefExoticComponent<(Props | S) & OptionalProps & { ref?: React.Ref<any> }>
|
|
159
|
+
return ForwardRefComponent as React.ForwardRefExoticComponent<(Props | S) & OptionalProps<StyleType> & { ref?: React.Ref<any> }>
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
return styledComponent
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ViewStyle, TextStyle } from 'react-native'
|
|
1
|
+
import { ViewStyle, TextStyle, ImageStyle } from 'react-native'
|
|
2
2
|
|
|
3
3
|
export type Units = {
|
|
4
4
|
'%'?: number;
|
|
@@ -54,7 +54,11 @@ export type Context = {
|
|
|
54
54
|
units: Units
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
export type
|
|
57
|
+
export type AnyStyle = ViewStyle | TextStyle | ImageStyle
|
|
58
|
+
|
|
59
|
+
export type CompleteStyle = ViewStyle & TextStyle & ImageStyle
|
|
60
|
+
|
|
61
|
+
export type PartialStyle = Partial<Record<keyof CompleteStyle, string>> & {
|
|
58
62
|
shadowOffset?: {
|
|
59
63
|
width: string;
|
|
60
64
|
height: string;
|