threlte logo
@threlte/flex

<Box>

The component <Box> creates a flexbox item. It can be used as a direct child of <Flex> or as a child of another <Box> to create a nested flex layout.

Usage

<script lang="ts">
  import { Flex, Box } from '@threlte/flex'
  import Model from './Model.svelte'
</script>

<Flex
  width={100}
  height={100}
  justifyContent="Center"
>
  <Box
    flex={1}
    width="auto"
  >
    {#snippet children({ width })}
      <Model {width} />
    {/snippet}
  </Box>
</Flex>

Content Sizing

The <Box> component controls element positions only. However, if you wish to handle element dimensions based on the layout calculated by Yoga, you’ll need to manually adapt the content’s size. This is because @threlte/flex lacks knowledge about the inner content’s sizing mechanisms. For this purpose, @threlte/flex offers the computed dimensions in three ways:

  • Using the width and height snippet props
<Box>
  {#snippet children({ width, height })}
    <T.Mesh
      scale.x={width}
      scale.y={height}
    />
  {/snippet}
</Box>
Child.svelte
<script lang="ts">
  import { useDimensions } from '@threlte/flex'
  const { width, height } = useDimensions()
</script>

<T.Mesh
  scale.x={$width}
  scale.y={$height}
/>
  • Using the reflow event
<Box
  onreflow={({ width, height }) => {
    console.log(width, height)
  }}
>
  <!-- … -->
</Box>

Layout Reflow

To trigger a layout reflow, you can use the reflow slot prop:

<script lang="ts">
  import { Box } from '@threlte/flex'
  import { Text } from '@threlte/extras'
</script>

<Box>
  {#snippet children({ reflow })}
    <Text text="Hello World" onsync={reflow}>
  {/snippet}
</Box>

Item Ordering

By default, the order of a flex item is determined by the order of insertion in the component tree. If for any reason you need to change the order of a flex item manually, you can use the order prop:

<script lang="ts">
  import { Box } from '@threlte/flex'
</script>

<Flex
  width={100}
  height={100}
>
  <Box order={2} />
  <Box order={1} />
  <Box order={3} />
</Flex>

Component Signature

Props

name
type
required
description

alignContent
"Auto" | "FlexStart" | "Center" | "FlexEnd" | "Stretch" | "Baseline" | "SpaceBetween" | "SpaceAround"
no

alignItems
"Auto" | "FlexStart" | "Center" | "FlexEnd" | "Stretch" | "Baseline" | "SpaceBetween" | "SpaceAround"
no

alignSelf
"Auto" | "FlexStart" | "Center" | "FlexEnd" | "Stretch" | "Baseline" | "SpaceBetween" | "SpaceAround"
no

aspectRatio
number
no

bottom
number | `${number}%`
no

class
string
no
declared classes will be resolved by a ClassParser declared on <Flex>

flex
number
no

flexBasis
number | "auto" | `${number}%`
no

flexDirection
"Column" | "ColumnReverse" | "Row" | "RowReverse"
no

flexGrow
number
no

flexShrink
number
no

flexWrap
"NoWrap" | "Wrap" | "WrapReverse"
no

gap
number
no

gapColumn
number
no

gapRow
number
no

height
number | "auto" | `${number}%`
no

justifyContent
"FlexStart" | "Center" | "FlexEnd" | "SpaceBetween" | "SpaceAround" | "SpaceEvenly"
no

left
number | `${number}%`
no

margin
number | "auto" | `${number}%`
no

marginBottom
number | "auto" | `${number}%`
no

marginLeft
number | "auto" | `${number}%`
no

marginRight
number | "auto" | `${number}%`
no

marginTop
number | "auto" | `${number}%`
no

maxHeight
number | `${number}%`
no

maxWidth
number | `${number}%`
no

minHeight
number | `${number}%`
no

minWidth
number | `${number}%`
no

order
number
no

padding
number | `${number}%`
no

paddingBottom
number | `${number}%`
no

paddingLeft
number | `${number}%`
no

paddingRight
number | `${number}%`
no

paddingTop
number | `${number}%`
no

right
number | `${number}%`
no

top
number | `${number}%`
no

width
number | "auto" | `${number}%`
no