Skip to content

convert arrays to template literals during transformation #3

@sastan

Description

@sastan

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 ?

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions