Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
feat: styled now accept strings too
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed Jan 1, 2021
1 parent 3b1c9f0 commit aeccddb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/__tests__/__fixtures__/simple/code.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import styled from '../../../../build/module/macro';

console.log(styled(['bg-white', 'selectable']));

console.log(styled(`bg-white selectable `));
7 changes: 7 additions & 0 deletions src/__tests__/__fixtures__/simple/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ const _styles = _StyleSheet.create({
_default: {
backgroundColor: 'white',
},
_default2: {
backgroundColor: 'white',
},
});

console.log({
selectable: true,
style: [_styles._default],
});
console.log({
selectable: true,
style: [_styles._default2],
});
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ type ComputedStyle<T extends Token> = UnionToIntersection<
>;

export type Macro = <T extends TokenWithVariant>(
tokens: T[],
tokens: T[] | string,
variants?: VariantFlags
) => ComputedStyle<ExtractToken<T>>;
6 changes: 5 additions & 1 deletion src/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const styledMacro: MacroHandler = ({ references, state }) => {
const callExpr = refPath.parent;

// Parse token argument
const { confident, value: tokens, deopt } = evalNode(
let { confident, value: tokens, deopt } = evalNode(
refPath.parentPath,
callExpr.arguments[0]
);
Expand All @@ -40,6 +40,10 @@ const styledMacro: MacroHandler = ({ references, state }) => {
);
}

if (typeof tokens === 'string') {
tokens = tokens.trim().replace(/\s+/g, ' ').split(' ');
}

if (!Array.isArray(tokens)) {
throw deopt?.buildCodeFrameError(
'style names must be an array of strings'
Expand Down

0 comments on commit aeccddb

Please sign in to comment.