Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #429 from gluestack/release/@gluestack-style/babel…
Browse files Browse the repository at this point in the history
[email protected]

Release/@gluestack style/babel plugin styled [email protected]
  • Loading branch information
ankit-tailor authored Sep 14, 2023
2 parents 1f6b77a + a65f22d commit 341dc2f
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 126 deletions.
63 changes: 29 additions & 34 deletions example/storybook/src/api/DescendantsStyles/ContextBasedStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@ import {
StyleSheet,
View,
} from 'react-native';
import {
AsForwarder,
createStyled,
styled1,
Theme,
} from '@gluestack-style/react';
import { AsForwarder, createStyled, styled } from '@gluestack-style/react';
import { Wrapper } from '../../components/Wrapper';
import { AddIcon, Box, Icon } from '@gluestack/design-system';
// import { AddIcon } from '@gluestack/design-system';
import { AlertCircle, Circle, Sun } from 'lucide-react-native';

import { AnimationResolver } from '@gluestack-style/animation-plugin';

const styled = createStyled([new AnimationResolver({})]);
const styleshet = StyleSheet.create({
style: {
padding: 12,
Expand All @@ -37,7 +31,6 @@ const Pressable = styled(
{
bg: '$red500',
p: '$2',

// 'bg': '$red600',
// 'w': 100,
// 'h': 100,
Expand Down Expand Up @@ -115,32 +108,34 @@ const Text = styled(
const StyledIcon = styled(
AsForwarder,
{
bg: '$red500',
variants: {
size: {
sm: {
width: 10,
height: 10,
// props: {
// size: 32,
// },
},
md: {
// props: {
// size: 32,
// },
width: '$4',
height: '$4',
},
lg: {
// props: {
// size: 32,
// },
width: '$6',
height: '$6',
},
},
},
bg: '$amber100',
bgColor: '$amber100',

// variants: {
// size: {
// sm: {
// width: 10,
// height: 10,
// // props: {
// // size: 32,
// // },
// },
// md: {
// // props: {
// // size: 32,
// // },
// width: '$4',
// height: '$4',
// },
// lg: {
// // props: {
// // size: 32,
// // },
// width: '$6',
// height: '$6',
// },
// },
// },
},
{
componentName: 'MyIcon',
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-styled-resolver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gluestack-style/babel-plugin-styled-resolver",
"version": "0.2.4",
"version": "0.2.5",
"description": "A gluestack-style babel plugin that transpiles your styled function calls and resolves the component styling in build time.",
"keywords": [
"css-in-js",
Expand Down
63 changes: 42 additions & 21 deletions packages/babel-plugin-styled-resolver/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,43 +97,64 @@ function addQuotesToObjectKeys(code) {
});

traverse(ast, {
ObjectProperty: (path) => {
if (types.isTemplateLiteral(path.node.value)) {
path.node.value = types.stringLiteral(
path.node.value.quasis[0].value.raw
ObjectProperty: (objectPropertyPath) => {
if (types.isTemplateLiteral(objectPropertyPath.node.value)) {
objectPropertyPath.node.value = types.stringLiteral(
objectPropertyPath.node.value.quasis[0].value.raw
);
}
if (types.isIdentifier(path.node.key)) {
path.node.key = types.stringLiteral(path.node.key.name);
if (types.isIdentifier(objectPropertyPath.node.key)) {
objectPropertyPath.node.key = types.stringLiteral(
objectPropertyPath.node.key.name
);
}
if (types.isNumericLiteral(path.node.key)) {
path.node.key = types.stringLiteral(path.node.key.extra.raw);
if (types.isNumericLiteral(objectPropertyPath.node.key)) {
objectPropertyPath.node.key = types.stringLiteral(
objectPropertyPath.node.key.extra.raw
);
}
if (types.isStringLiteral(path.node.value)) {
path.node.value = types.stringLiteral(path.node.value.value);
if (types.isStringLiteral(objectPropertyPath.node.value)) {
objectPropertyPath.node.value = types.stringLiteral(
objectPropertyPath.node.value.value
);
}
},
});

let initAst;

traverse(ast, {
ObjectProperty: (path) => {
if (types.isIdentifier(path?.node?.value)) {
path.remove();
}
if (types.isTemplateLiteral(path?.node?.value)) {
path.remove();
}
if (types.isConditionalExpression(path?.node?.value)) {
path.remove();
ObjectProperty: (objectPropertyPath) => {
if (types.isArrayExpression(objectPropertyPath?.node?.value)) {
let arrayElements = objectPropertyPath.node.value.elements;
const dynamicElementsIndex = [];
arrayElements.forEach((element, index) => {
if (
types.isNewExpression(element) ||
types.isIdentifier(element) ||
types.isTemplateLiteral(element)
) {
dynamicElementsIndex.push(index);
}
});

arrayElements = arrayElements.filter(
(element, index) => !dynamicElementsIndex.includes(index)
);
objectPropertyPath.node.value.elements = arrayElements;
} else if (
types.isIdentifier(objectPropertyPath?.node?.value) ||
types.isTemplateLiteral(objectPropertyPath?.node?.value) ||
types.isConditionalExpression(objectPropertyPath?.node?.value)
) {
objectPropertyPath.remove();
}
},
});

traverse(ast, {
VariableDeclarator: (path) => {
initAst = path.node.init;
VariableDeclarator: (variableDeclaratorPath) => {
initAst = variableDeclaratorPath.node.init;
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gluestack-style/react",
"description": "A universal & performant styling library for React Native, Next.js & React",
"version": "0.2.43",
"version": "0.2.45",
"keywords": [
"React Native",
"Next.js",
Expand Down
14 changes: 11 additions & 3 deletions packages/react/src/AsForwarder.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import React from 'react';
import Svg from 'react-native-svg';
import type { RNProps } from './types';

const AsForwarder = ({ as, children, ...props }: any) => {
const AsForwarderTemp = ({
as,
children,
...props
}: any): React.ReactElement => {
const As: any = as;
return as ? <As {...props}>{children}</As> : <Svg {...props}>{children}</Svg>;
};
AsForwarder.displayName = '__AsForwarder__';
export { AsForwarder };
AsForwarderTemp.displayName = '__AsForwarder__';

export const AsForwarder = AsForwarderTemp as React.ComponentType<
RNProps & { as?: any }
>;
4 changes: 2 additions & 2 deletions packages/react/src/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ const getStyleIdsFromMap = (
return componentStyleObject;
};

export function verboseStyled<P, Variants, ComCon, PluginType>(
export function verboseStyled<P, Variants, ComCon, PluginType = unknown>(
Component: React.ComponentType<P>,
theme: Partial<IVerbosedTheme<Variants, P>>,
componentStyleConfig: IComponentStyleConfig<ComCon> = {},
Expand Down Expand Up @@ -1826,7 +1826,7 @@ export function verboseStyled<P, Variants, ComCon, PluginType>(
return StyledComp;
}

export function styled<P, Variants, ComCon, PluginType = []>(
export function styled<P, Variants, ComCon, PluginType = unknown>(
Component: React.ComponentType<P>,
theme: ITheme<Variants, P, PluginType>,
componentStyleConfig?: IComponentStyleConfig<ComCon>,
Expand Down
112 changes: 48 additions & 64 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ type PassingPropsType<
}
: {};

// PluginPropsType<
// PluginType,
// GenericComponentProps,
// GenericComponentStyles,
// PLATFORM
// >

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type PluginPropsType<
PluginType,
GenericComponentProps,
Expand Down Expand Up @@ -398,7 +406,7 @@ export type SxProps<
GenericComponentProps = unknown,
PLATFORM = '',
MediaQuery = '',
PluginType = []
_PluginType = []
> = Partial<
StylePropsType<GenericComponentStyles, PLATFORM> &
PassingPropsType<
Expand All @@ -407,81 +415,57 @@ export type SxProps<
GenericComponentProps,
MediaQuery
>
> &
PluginPropsType<
PluginType,
> & {
[Key in `_${COLORMODES}`]?: SxProps<
GenericComponentStyles,
Variants,
GenericComponentProps,
PLATFORM,
MediaQuery
>;
} & {
[Key in `:${IState}`]?: SxProps<
GenericComponentStyles,
PLATFORM
> & {
[Key in `_${COLORMODES}`]?: SxProps<
GenericComponentStyles,
Variants,
GenericComponentProps,
PLATFORM,
MediaQuery
> &
PluginPropsType<
PluginType,
GenericComponentProps,
GenericComponentStyles,
PLATFORM
>;
} & {
[Key in `:${IState}`]?: SxProps<
Variants,
GenericComponentProps,
PLATFORM,
MediaQuery
>;
} & {
[Key in `_${PLATFORMS}`]?: SxProps<
GenericComponentStyles,
Variants,
GenericComponentProps,
Key,
MediaQuery
> &
PassingPropsType<
GenericComponentStyles,
Variants,
GenericComponentProps,
PLATFORM,
MediaQuery
> &
PluginPropsType<
PluginType,
GenericComponentProps,
GenericComponentStyles,
PLATFORM
>;
} & {
[Key in `_${PLATFORMS}`]?: SxProps<
Partial<{
[key: string]: any;
}>;
} & {
[Key in `_${string}`]?: SxProps<
RNStyledProps,
{},
GenericComponentProps,
PLATFORM,
MediaQuery
> &
PassingPropsType<
GenericComponentStyles,
Variants,
GenericComponentProps,
Key,
MediaQuery
> &
PluginPropsType<
PluginType,
GenericComponentProps,
GenericComponentStyles,
PLATFORM
> &
PassingPropsType<
GenericComponentStyles,
Variants,
GenericComponentProps,
MediaQuery
> &
Partial<{
[key: string]: any;
}>;
} & {
[Key in `_${string}`]?: SxProps<
RNStyledProps,
{},
GenericComponentProps,
PLATFORM,
MediaQuery
> &
PassingPropsType<
GenericComponentStyles,
{},
GenericComponentProps,
MediaQuery
> &
Partial<{
[key: string]: any;
}>;
};
Partial<{
[key: string]: any;
}>;
};

export type VariantType<
Variants,
Expand Down

0 comments on commit 341dc2f

Please sign in to comment.