threlte logo
@threlte/flex

tailwindParser

@threlte/flex ships with a default parser for Tailwind-like classes on <Flex> and <Box> components. If you are familiar with Tailwind, you will feel right at home.

<Flex
  width={300}
  height={300}
  classParser={tailwindParser}
  class="flex-col items-center justify-center gap-10 p-10"
>
  <Box class="w-100 h-full shrink-0">
    {#snippet children({ width, height })}
      <Plane
        {width}
        {height}
        color="yellow"
        depth={1}
      />
    {/snippet}
  </Box>

  <Box class="flex-1">
    {#snippet children({ width })}
      <Plane
        color="blue"
        {width}
        height={44}
        depth={1}
      />
    {/snippet}
  </Box>
</Flex>

Usage

Add the classParser prop to your <Flex> or <Box> component and pass in the tailwindParser function.

<script>
  import { Flex, Box, Plane, tailwindParser } from '@threlte/flex'
</script>

<Flex
  width={300}
  height={300}
  classParser={tailwindParser}
  class="flex-col items-center justify-center gap-10 p-10"
>
  <Box class="h-100 w-100">
    {#snippet children({ width, height })}
      <!-- … -->
    {/snippet}
  </Box>
</Flex>

Differences to Tailwind

The syntax is very similar to Tailwind, with a few differences:

  • If a value is a number, it is interpreted as a unit in Three.js. For example, gap-10 will set the gap to 10 Three.js units.
  • As opposed to Tailwind, you are not limited to the default set of values. You can use any value: gap-17.5 will set the gap to 17.5 Three.js units. p-56% will set the padding to 56% of the parent’s width. Do not wrap the value in brackets as you would do with custom values in Tailwind CSS.

Supported Props

The following props are supported by the Tailwind parser:

Flex Direction

ClassCSS Properties
flex-rowflex-direction: row
flex-colflex-direction: column
flex-row-reverseflex-direction: row-reverse
flex-col-reverseflex-direction: column-reverse

Flex Wrap

ClassCSS Properties
flex-wrapflex-wrap: wrap
flex-wrap-reverseflex-wrap: wrap-reverse
flex-nowrapflex-wrap: nowrap

Justify Content

ClassCSS Properties
justify-startjustify-content: flex-start
justify-endjustify-content: flex-end
justify-centerjustify-content: center
justify-betweenjustify-content: space-between
justify-aroundjustify-content: space-around
justify-evenlyjustify-content: space-evenly

Align Items

ClassCSS Properties
items-startalign-items: flex-start
items-endalign-items: flex-end
items-centeralign-items: center
items-baselinealign-items: baseline
items-stretchalign-items: stretch

Align Content

ClassCSS Properties
content-normal align-content: normal
content-startalign-content: flex-start
content-endalign-content: flex-end
content-centeralign-content: center
content-betweenalign-content: space-between
content-aroundalign-content: space-around
content-stretchalign-content: stretch
content-baselinealign-content: baseline

Gap

value is a number (e.g. 10)

ClassCSS PropertiesExamples
gap-{value}gapgap-10
gap-x-{value}column-gapgap-x-10
gap-y-{value}row-gapgap-y-10

Flex Grow

value is a number (e.g. 1)

ClassCSS PropertiesExamples
grow-{value}flex-growgrow-1, grow-0

Flex Shrink

value is a number (e.g. 1)

ClassCSS PropertiesExamples
shrink-{value}flex-shrinkshrink-1, shrink-0

Flex Basis

value can be auto, full, a percentage (e.g. 55%) or a number (e.g. 10).

ClassCSS PropertiesExamples
basis-{value}flex-basisbasis-10, basis-full, basis-55%, basis-auto

Flex

value is a number (e.g. 1)

ClassCSS PropertiesExamples
flex-{value}flexflex-1, flex-0.5

Align Self

ClassCSS Properties
self-autoalign-self: auto
self-startalign-self: flex-start
self-endalign-self: flex-end
self-centeralign-self: center
self-stretchalign-self: stretch
self-baselinealign-self: baseline

Padding

value can be full, a percentage (e.g. 55%) or a number (e.g. 10).

ClassCSS PropertiesExamples
p-{value}paddingp-10, p-full, p-55%
pt-{value}padding-toppt-10, pt-full, pt-55%
pr-{value}padding-rightpr-10, pr-full, pr-55%
pb-{value}padding-bottompb-10, pb-full, pb-55%
pl-{value}padding-leftpl-10, pl-full, pl-55%
px-{value}padding-left
padding-right
px-10, px-full, px-55%
py-{value}padding-top
padding-bottom
py-10, py-full, py-55%

Margin

value can be full, a percentage (e.g. 55%) or a number (e.g. 10).

ClassCSS PropertiesExamples
m-{value}marginm-10, m-full, m-55%
mt-{value}margin-topmt-10, mt-full, mt-55%
mr-{value}margin-rightmr-10, mr-full, mr-55%
mb-{value}margin-bottommb-10, mb-full, mb-55%
ml-{value}margin-leftml-10, ml-full, ml-55%
mx-{value}margin-left
margin-right
mx-10, mx-full, mx-55%
my-{value}margin-top
margin-bottom
my-10, my-full, my-55%

Width

value can be auto, full, a percentage (e.g. 55%) or a number (e.g. 10).

ClassCSS PropertiesExamples
w-{value}widthw-10, w-full, w-55%, w-auto

Height

value can be auto, full, a percentage (e.g. 55%) or a number (e.g. 10).

ClassCSS PropertiesExamples
h-{value}heighth-10, h-full, h-55%, h-auto

Top, Right, Bottom, Left

value can be a percentage (e.g. 55%) or a number (e.g. 10).

ClassCSS PropertiesExamples
top-{value}toptop-10, top-55%
right-{value}rightright-10, right-55%
bottom-{value}bottombottom-10, bottom-55%
left-{value}leftleft-10, left-55%

Aspect Ratio

value can be square, portrait, landscape or an arbitrary ratio (e.g. 32/9).

ClassCSS PropertiesExamples
aspect-square aspect-ratio: 1 / 1
aspect-portraitaspect-ratio: 9 / 16
aspect-landscapeaspect-ratio: 16 / 9
aspect-{value}aspect-ratio: {value}aspect-32/9, aspect-2/1