Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert arrays to template literals during transformation #3

Open
sastan opened this issue Jan 6, 2021 · 2 comments
Open

convert arrays to template literals during transformation #3

sastan opened this issue Jan 6, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@sastan
Copy link
Contributor

sastan commented Jan 6, 2021

This must not be resolved before the initial release.

Twind recommends using template literal syntax as it allows advanced caching. Is it possible to transform arrays to a template literal call of tw?

<div tw={['bg-gray-200', true && 'rounded', { hover: ['bg-red-500', 'p-3'] }, 'underline']}></div>
// => tw`bg-blue-500 ${true && 'rounded'} ${{ hover: ['bg-red-500', 'p-3'] }} underline`

That would mean all strings are converted to template elements and non-strings are interpolations values (see astexplorer).

This would should work for static strings as well:

<div tw="bg-gray-200 rounded"></div>
// => tw`bg-gray-200 rounded`

If the tw attribute value is already a template literal that could be used as is:

<div tw={`bg-blue-500 ${true && 'rounded'} ${{ hover: ['bg-red-500', 'p-3'] }} underline`}></div>
// => tw`bg-blue-500 ${true && 'rounded'} ${{ hover: ['bg-red-500', 'p-3'] }} underline`

For non-array and non-string tw attribute values i would recommend to call tw with the value as is.

<div tw={{ 'bg-gray-200': true, rounded: false, underline: isTrue() }}></div>
// => tw({ 'bg-gray-200': true, rounded: false, underline: isTrue() })

What to you think @itsMapleLeaf ?

@itsMapleLeaf
Copy link
Contributor

Yep, this can be done. Anyone else is welcome to pick this up before I do. I'll say if/when I can get to it

@itsMapleLeaf itsMapleLeaf self-assigned this Jan 10, 2021
@sastan sastan transferred this issue from tw-in-js/twind.macro Mar 4, 2021
@itsMapleLeaf
Copy link
Contributor

Most of this is done as of v1.0.1, only missing this case:

<div tw={['bg-gray-200', true && 'rounded', { hover: ['bg-red-500', 'p-3'] }, 'underline']}></div>

// currently:
// => tw`${'bg-blue-500'} ${true && 'rounded'} ${{ hover: ['bg-red-500', 'p-3'] }} ${'underline'}`

// desired:
// => tw`bg-blue-500 ${true && 'rounded'} ${{ hover: ['bg-red-500', 'p-3'] }} underline`

@itsMapleLeaf itsMapleLeaf added the enhancement New feature or request label Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants