Skip to content

Output tw.apply calls #2

@itsMapleLeaf

Description

@itsMapleLeaf

Pending this proposal

Here's the gist:

// input
import '@twind/macro'

const jsx = <button tw="bg-blue-500" />

// output, as of now
import { tw } from 'twind'

const jsx = <button className={tw`bg-blue-500`} />

// rough proposed output, aliasing to avoid conflicts
import { tw as _tw } from 'twind'
const _macro_tw = (...args) => _tw(_tw.apply(...args))

const jsx = <button className={_macro_tw`bg-blue-500`} />

This gives the benefit of reliable styling depending on class name order when using the macro, something that would be especially welcome for users coming from twin.macro and other CSS in JS libs. This should probably also apply to standalone tw usages that are imported from the macro.

At first, I'm thinking this would be an option/flag for the macro, but I'm wondering if there's any reason it shouldn't do this by default 🤔 Would this negatively impact performance? I guess it's hard to know for sure without measurements. cc @sastan

I also realized (right after making the issue 🙃) that the hashed class names would make debugging a little harder. One way to fix that would be to add an extra attribute to the element that shows the actual classes used, so it'll show up in the DOM as <button class="tw-abcxyz" data-tw="bg-blue-500" />

Metadata

Metadata

Assignees

No one assigned

    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