# Select

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Select defaultValue="apple" class="w-[200px]"> <SelectTrigger> <SelectValue placeholder="Select a fruit" /> </SelectTrigger> <SelectContent> <SelectItem value="apple">{"Apple"}</SelectItem> <SelectItem value="banana">{"Banana"}</SelectItem> <SelectItem value="cherry">{"Cherry"}</SelectItem> </SelectContent> </Select>
</div>

```astro
---
---

<Select defaultValue="apple" class="w-[200px]">
  <SelectTrigger>
    <SelectValue placeholder="Select a fruit" />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="apple">Apple</SelectItem>
    <SelectItem value="banana">Banana</SelectItem>
    <SelectItem value="cherry">Cherry</SelectItem>
  </SelectContent>
</Select>
```

## Installation

<DocsTabs syncKey="pkg">
  <DocsTabItem label="bun">
  ```bash
  bunx bejamas add select
  ```
  </DocsTabItem>
  <DocsTabItem label="npm">
  ```bash
  npx bejamas add select
  ```
  </DocsTabItem>
  <DocsTabItem label="pnpm">
  ```bash
  pnpm dlx bejamas add select
  ```
  </DocsTabItem>
  <DocsTabItem label="yarn">
  ```bash
  yarn dlx bejamas add select
  ```
  </DocsTabItem>
</DocsTabs>

## Usage

```astro nocollapse
---
---

<Select name="fruit" defaultValue="apple">
  <SelectTrigger>
    <SelectValue placeholder="Select a fruit" />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="apple">Apple</SelectItem>
    <SelectItem value="banana">Banana</SelectItem>
  </SelectContent>
</Select>
```

## Props

| Prop | Type | Default |
|---|---|---|
| <code>class</code> | `string` | `""` |
| <code>name</code> | `string` |  |
| <code>defaultValue</code> | `string` |  |
| <code>placeholder</code> | `string` |  |
| <code>disabled</code> | `boolean` | `false` |
| <code>required</code> | `boolean` | `false` |
| <code>size</code> | `"sm" \| "default" \| "lg"` | `"default"` |

## Examples

### With Groups

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Select class="w-[200px]"> <SelectTrigger> <SelectValue placeholder="Select food" /> </SelectTrigger> <SelectContent> <SelectGroup> <SelectLabel>{"Fruits"}</SelectLabel> <SelectItem value="apple">{"Apple"}</SelectItem> <SelectItem value="banana">{"Banana"}</SelectItem> </SelectGroup> <SelectSeparator /> <SelectGroup> <SelectLabel>{"Vegetables"}</SelectLabel> <SelectItem value="carrot">{"Carrot"}</SelectItem> <SelectItem value="broccoli">{"Broccoli"}</SelectItem> </SelectGroup> </SelectContent> </Select>
</div>

```astro
---
---

<Select class="w-[200px]">
  <SelectTrigger>
    <SelectValue placeholder="Select food" />
  </SelectTrigger>
  <SelectContent>
    <SelectGroup>
      <SelectLabel>Fruits</SelectLabel>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
    </SelectGroup>
    <SelectSeparator />
    <SelectGroup>
      <SelectLabel>Vegetables</SelectLabel>
      <SelectItem value="carrot">Carrot</SelectItem>
      <SelectItem value="broccoli">Broccoli</SelectItem>
    </SelectGroup>
  </SelectContent>
</Select>
```

### Sizes

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<div class="flex flex-col items-start gap-4 sm:flex-row" > <Select class="w-[180px]" size="sm"> <SelectTrigger> <SelectValue placeholder="Small (h-8)" /> </SelectTrigger> <SelectContent> <SelectItem value="apple">{"Apple"}</SelectItem> <SelectItem value="banana">{"Banana"}</SelectItem> <SelectItem value="cherry">{"Cherry"}</SelectItem> </SelectContent> </Select> <Select class="w-[180px]" size="default"> <SelectTrigger> <SelectValue placeholder="Default (h-9)" /> </SelectTrigger> <SelectContent> <SelectItem value="apple">{"Apple"}</SelectItem> <SelectItem value="banana">{"Banana"}</SelectItem> <SelectItem value="cherry">{"Cherry"}</SelectItem> </SelectContent> </Select> <Select class="w-[180px]" size="lg"> <SelectTrigger size="lg"> <SelectValue placeholder="Large (h-10)" /> </SelectTrigger> <SelectContent> <SelectItem value="apple">{"Apple"}</SelectItem> <SelectItem value="banana">{"Banana"}</SelectItem> <SelectItem value="cherry">{"Cherry"}</SelectItem> </SelectContent> </Select> </div>
</div>

```astro
---
---

<div class="flex flex-col items-start gap-4 sm:flex-row" >
  <Select class="w-[180px]" size="sm">
    <SelectTrigger>
      <SelectValue placeholder="Small (h-8)" />
    </SelectTrigger>
    <SelectContent>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
      <SelectItem value="cherry">Cherry</SelectItem>
    </SelectContent>
  </Select>
  <Select class="w-[180px]" size="default">
    <SelectTrigger>
      <SelectValue placeholder="Default (h-9)" />
    </SelectTrigger>
    <SelectContent>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
      <SelectItem value="cherry">Cherry</SelectItem>
    </SelectContent>
  </Select>
  <Select class="w-[180px]" size="lg">
    <SelectTrigger size="lg">
      <SelectValue placeholder="Large (h-10)" />
    </SelectTrigger>
    <SelectContent>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
      <SelectItem value="cherry">Cherry</SelectItem>
    </SelectContent>
  </Select>
</div>
```

### Disabled

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Select disabled class="w-[200px]"> <SelectTrigger> <SelectValue placeholder="Disabled" /> </SelectTrigger> <SelectContent> <SelectItem value="apple">{"Apple"}</SelectItem> <SelectItem value="banana">{"Banana"}</SelectItem> <SelectItem value="cherry">{"Cherry"}</SelectItem> </SelectContent> </Select>
</div>

```astro
---
---

<Select disabled class="w-[200px]">
  <SelectTrigger>
    <SelectValue placeholder="Disabled" />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="apple">Apple</SelectItem>
    <SelectItem value="banana">Banana</SelectItem>
    <SelectItem value="cherry">Cherry</SelectItem>
  </SelectContent>
</Select>
```

### With Disabled Items

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Select class="w-[200px]" placeholder="Choose..."> <SelectTrigger> <SelectValue placeholder="Choose..." /> </SelectTrigger> <SelectContent> <SelectItem value="available">{"Available"}</SelectItem> <SelectItem value="unavailable" disabled>{"Unavailable"}</SelectItem> <SelectItem value="another">{"Another option"}</SelectItem> </SelectContent> </Select>
</div>

```astro
---
---

<Select class="w-[200px]" placeholder="Choose...">
  <SelectTrigger>
    <SelectValue placeholder="Choose..." />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="available">Available</SelectItem>
    <SelectItem value="unavailable" disabled>Unavailable</SelectItem>
    <SelectItem value="another">Another option</SelectItem>
  </SelectContent>
</Select>
```

### Popper Mode

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<div class="flex flex-wrap items-start gap-4"> <Select class="w-[160px]"> <SelectTrigger> <SelectValue placeholder="Bottom" /> </SelectTrigger> <SelectContent position="popper" side="bottom"> <SelectItem value="option1">{"Option 1"}</SelectItem> <SelectItem value="option2">{"Option 2"}</SelectItem> <SelectItem value="option3">{"Option 3"}</SelectItem> </SelectContent> </Select> <Select class="w-[160px]"> <SelectTrigger> <SelectValue placeholder="Top" /> </SelectTrigger> <SelectContent position="popper" side="top"> <SelectItem value="option1">{"Option 1"}</SelectItem> <SelectItem value="option2">{"Option 2"}</SelectItem> <SelectItem value="option3">{"Option 3"}</SelectItem> </SelectContent> </Select> </div>
</div>

```astro
---
---

<div class="flex flex-wrap items-start gap-4">
  <Select class="w-[160px]">
    <SelectTrigger>
      <SelectValue placeholder="Bottom" />
    </SelectTrigger>
    <SelectContent position="popper" side="bottom">
      <SelectItem value="option1">Option 1</SelectItem>
      <SelectItem value="option2">Option 2</SelectItem>
      <SelectItem value="option3">Option 3</SelectItem>
    </SelectContent>
  </Select>
  <Select class="w-[160px]">
    <SelectTrigger>
      <SelectValue placeholder="Top" />
    </SelectTrigger>
    <SelectContent position="popper" side="top">
      <SelectItem value="option1">Option 1</SelectItem>
      <SelectItem value="option2">Option 2</SelectItem>
      <SelectItem value="option3">Option 3</SelectItem>
    </SelectContent>
  </Select>
</div>
```

## API Reference

### Events

The select emits custom events that you can listen to:

| Event | Detail | Description |
|-------|--------|-------------|
| `select:change` | `{ value: string }` | Fired when a new value is selected |
| `select:open-change` | `{ open: boolean }` | Fired when the dropdown opens or closes |

<div id="sl-bejamas-console-preview-1" class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Select id="my-select"> <SelectTrigger> <SelectValue placeholder="Choose..." /> </SelectTrigger> <SelectContent> <SelectItem value="option1">{"Option 1"}</SelectItem> <SelectItem value="option2">{"Option 2"}</SelectItem> </SelectContent> </Select>
</div>
<div class="not-content sl-bejamas-console-log-shell px-4 md:px-10 py-4 border border-border border-t-0">
<pre id="sl-bejamas-console-preview-1-output" data-slot="event-log" class="sl-bejamas-console-log w-full p-3 rounded-md bg-muted text-xs font-mono text-muted-foreground min-h-[80px] max-h-[200px] overflow-y-auto">Waiting for logs...</pre>
<script type="module" src="data:text/javascript;charset=utf-8,(function%20()%20%7B%0A%20%20var%20root%20%3D%20document.getElementById(%22sl-bejamas-console-preview-1%22)%3B%0A%20%20var%20panel%20%3D%20document.getElementById(%22sl-bejamas-console-preview-1-output%22)%3B%0A%20%20if%20(!root%20%7C%7C%20!panel)%20return%3B%0A%0A%20%20var%20placeholder%20%3D%20panel.textContent%20%7C%7C%20%22Waiting%20for%20logs...%22%3B%0A%20%20var%20hasLogs%20%3D%20false%3B%0A%0A%20%20var%20toText%20%3D%20function%20(value)%20%7B%0A%20%20%20%20if%20(typeof%20value%20%3D%3D%3D%20%22string%22)%20return%20value%3B%0A%20%20%20%20if%20(value%20%3D%3D%3D%20undefined)%20return%20%22undefined%22%3B%0A%20%20%20%20if%20(value%20%3D%3D%3D%20null)%20return%20%22null%22%3B%0A%20%20%20%20try%20%7B%0A%20%20%20%20%20%20return%20JSON.stringify(value)%3B%0A%20%20%20%20%7D%20catch%20(_error)%20%7B%0A%20%20%20%20%20%20return%20String(value)%3B%0A%20%20%20%20%7D%0A%20%20%7D%3B%0A%0A%20%20var%20append%20%3D%20function%20(level%2C%20args)%20%7B%0A%20%20%20%20var%20stamp%20%3D%20new%20Date().toLocaleTimeString()%3B%0A%20%20%20%20var%20body%20%3D%20Array.prototype.map.call(args%2C%20toText).join(%22%20%22)%3B%0A%20%20%20%20var%20line%20%3D%20%22%5B%22%20%2B%20stamp%20%2B%20%22%5D%20%22%20%2B%20String(level).toUpperCase()%20%2B%20%22%3A%20%22%20%2B%20body%3B%0A%20%20%20%20var%20current%20%3D%20panel.textContent%20%7C%7C%20%22%22%3B%0A%20%20%20%20if%20(!hasLogs%20%26%26%20current.trim()%20%3D%3D%3D%20placeholder.trim())%20%7B%0A%20%20%20%20%20%20panel.textContent%20%3D%20line%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20panel.textContent%20%3D%20current%20%3F%20current%20%2B%20%22%5Cn%22%20%2B%20line%20%3A%20line%3B%0A%20%20%20%20%7D%0A%20%20%20%20hasLogs%20%3D%20true%3B%0A%20%20%20%20panel.scrollTop%20%3D%20panel.scrollHeight%3B%0A%20%20%7D%3B%0A%0A%20%20var%20methods%20%3D%20%5B%22log%22%2C%20%22info%22%2C%20%22warn%22%2C%20%22error%22%5D%3B%0A%20%20var%20proxyConsole%20%3D%20Object.create(console)%3B%0A%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20methods.length%3B%20i%20%2B%3D%201)%20%7B%0A%20%20%20%20(function%20(methodName)%20%7B%0A%20%20%20%20%20%20var%20fallback%20%3D%20typeof%20console.log%20%3D%3D%3D%20%22function%22%20%3F%20console.log.bind(console)%20%3A%20function%20()%20%7B%7D%3B%0A%20%20%20%20%20%20var%20original%20%3D%0A%20%20%20%20%20%20%20%20typeof%20console%5BmethodName%5D%20%3D%3D%3D%20%22function%22%0A%20%20%20%20%20%20%20%20%20%20%3F%20console%5BmethodName%5D.bind(console)%0A%20%20%20%20%20%20%20%20%20%20%3A%20fallback%3B%0A%20%20%20%20%20%20proxyConsole%5BmethodName%5D%20%3D%20function%20()%20%7B%0A%20%20%20%20%20%20%20%20var%20args%20%3D%20Array.prototype.slice.call(arguments)%3B%0A%20%20%20%20%20%20%20%20original.apply(console%2C%20args)%3B%0A%20%20%20%20%20%20%20%20append(methodName%2C%20args)%3B%0A%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%7D)(methods%5Bi%5D)%3B%0A%20%20%7D%0A%0A%20%20try%20%7B%0A%20%20%20%20var%20run%20%3D%20new%20Function(%22console%22%2C%20%22root%22%2C%20%22panel%22%2C%20%22const%20select%20%3D%20document.getElementById('my-select')%3B%5Cn%5Cn%20%20select.addEventListener('select%3Achange'%2C%20(e)%20%3D%3E%20%7B%5Cn%20%20%20%20console.log('Selected%20value%3A'%2C%20e.detail.value)%3B%5Cn%20%20%7D)%3B%5Cn%5Cn%20%20select.addEventListener('select%3Aopen-change'%2C%20(e)%20%3D%3E%20%7B%5Cn%20%20%20%20console.log('Is%20open%3A'%2C%20e.detail.open)%3B%5Cn%20%20%7D)%3B%22)%3B%0A%20%20%20%20run(proxyConsole%2C%20root%2C%20panel)%3B%0A%20%20%7D%20catch%20(error)%20%7B%0A%20%20%20%20var%20fallbackError%20%3D%20typeof%20console.error%20%3D%3D%3D%20%22function%22%20%3F%20console.error.bind(console)%20%3A%20function%20()%20%7B%7D%3B%0A%20%20%20%20fallbackError(error)%3B%0A%20%20%20%20append(%22error%22%2C%20%5Berror%5D)%3B%0A%20%20%7D%0A%7D)()%3B"></script>
</div>

```astro nocollapse console
<Select id="my-select">
  <SelectTrigger>
    <SelectValue placeholder="Choose..." />
  </SelectTrigger>
  <SelectContent>
    <SelectItem value="option1">Option 1</SelectItem>
    <SelectItem value="option2">Option 2</SelectItem>
  </SelectContent>
</Select>

<script>
  const select = document.getElementById('my-select');

  select.addEventListener('select:change', (e) => {
    console.log('Selected value:', e.detail.value);
  });

  select.addEventListener('select:open-change', (e) => {
    console.log('Is open:', e.detail.open);
  });
</script>
```

### Programmatic Control

You can control the select programmatically by dispatching a `select:set` event:

```js nocollapse
const select = document.getElementById('my-select');

// Set a specific value
select.dispatchEvent(new CustomEvent('select:set', {
  detail: { value: 'option2' }
}));

// Open the select dropdown
select.dispatchEvent(new CustomEvent('select:set', {
  detail: { open: true }
}));

// Close the select dropdown
select.dispatchEvent(new CustomEvent('select:set', {
  detail: { open: false }
}));
```

### Data Attributes

The select sets these data attributes that you can use for styling or querying state:

| Attribute | Element | Description |
|-----------|---------|-------------|
| `data-state` | select, select-trigger, select-content | Current state (`open` or `closed`) |
| `data-open` / `data-closed` | select, select-trigger, select-content | Presence attributes for style-driven open/close animations |
| `data-value` | select | Currently selected value |
| `data-selected` | select-item | Present when item is selected |
| `data-highlighted` | select-item | Present when item is focused/highlighted |
| `data-disabled` | select, select-trigger, select-item | Present when disabled |
| `data-placeholder` | select-trigger | Present when showing placeholder (no value selected) |
| `data-align-trigger` | select-content | Present when item-aligned positioning is active |
| `data-position` | select-content, select-viewport | Positioning mode (`item-aligned` or `popper`) |

### Position Modes

The `position` prop on `SelectContent` controls how the dropdown is positioned:

- **`item-aligned`** (default): The dropdown aligns with the selected item, similar to native select behavior
- **`popper`**: Uses floating UI positioning with configurable placement options on `SelectContent`

```astro nocollapse nopreview
<!-- Item-aligned (default) -->
<Select>
  <SelectContent>...</SelectContent>
</Select>

<!-- Popper mode -->
<Select>
  <SelectContent position="popper">...</SelectContent>
</Select>

<!-- Custom positioning on content -->
<Select>
  <SelectContent position="popper" side="bottom" align="start" sideOffset={8}>
    ...
  </SelectContent>
</Select>
```