Files
livewire-material/resources/views/components/collapse.blade.php
T
Andreas Reinhold / reiniandClaude Opus 5 a237d33426 Ease a collapse open, which its own comment already promised
Plan step 19, containment.md C-24. `interpolate-size: allow-keywords`
was set and nothing transitioned, so the content snapped open. A new
resources/css/components/collapse.css transitions `block-size` on
`::details-content` over the fast spatial spring, with
`content-visibility` `allow-discrete` beside it so the section stays
rendered while it closes. The `<details>` carries `data-collapse` for
the rule to find.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qwx5USif3wFFmxtHg5U1g9
2026-09-14 06:09:23 +02:00

72 lines
3.3 KiB
PHP

{{-- A section that opens to show more: an FAQ answer, advanced settings.
Not an M3 component; built on the native `<details>` so it opens without script, keeps its
state through a Livewire morph (`wire:ignore.self` stops the server's HTML from closing it),
and is announced as a disclosure. Drawn in M3's terms: a title-medium `title` (or a
`heading` slot), an optional leading `icon`, a chevron that turns over on the spatial spring,
and where the browser supports animating `details` content (`interpolate-size`) a height
that eases open on the same spring (`data-collapse`, resources/css/components/collapse.css).
`open` starts it open. `variant`: `plain` (the default, on the surface around
it) or `filled` (a surface-container tile with a large corner).
Its open state can be bound, both ways:
- `wire:model` (any modifiers; `.live` tells the server at once) entangles it with a Livewire
boolean property. The server renders `open` from the property, so the first paint matches it
and `open` is ignored; toggling writes the property, and the property changing opens or
closes it.
- `x-model` binds an Alpine property through `x-modelable`; `open` is then only the first
paint, until Alpine starts and applies the property.
The state lives in `collapseOpen` on the `<details>`, a name kept clear of `open`, which a
dialog inside the slot may be reading from a scope around it. Without a binding there is no
Alpine on it at all. --}}
@props([
'title' => null,
'icon' => null,
'open' => false,
'variant' => 'plain',
])
@php
$model = $attributes->wire('model')->value() ?: null;
$bound = $model !== null || count($attributes->whereStartsWith('x-model')->getAttributes()) > 0;
$expanded = (bool) $open;
if ($model !== null && ($component = \Livewire\Livewire::current()) !== null) {
$expanded = (bool) data_get($component, $model);
}
@endphp
<details
wire:ignore.self
data-collapse
@if ($bound)
x-data="{ collapseOpen: @if ($model !== null) @entangle($attributes->wire('model')) @else @js($expanded) @endif }"
@if ($model === null) x-modelable="collapseOpen" @endif
x-effect="$el.open = collapseOpen"
x-on:toggle="collapseOpen = $el.open"
@endif
@if ($expanded) open @endif
{{ $attributes->whereDoesntStartWith('wire:model')->class([
'group/collapse [interpolate-size:allow-keywords]',
'rounded-corner-lg bg-surface-container' => $variant === 'filled',
]) }}
>
<summary @class([
'state-layer focus-ring flex min-h-12 cursor-pointer list-none items-center gap-3 rounded-[inherit] type-title-md [&::-webkit-details-marker]:hidden',
'px-4' => $variant === 'filled',
])>
@if ($icon)
<x-livewire-material::icon :name="$icon" class="size-6 text-on-surface-variant" />
@endif
<span class="min-w-0 flex-1">{{ $heading ?? $title }}</span>
<x-livewire-material::icon name="expand_more" class="size-6 text-on-surface-variant transition-[rotate] duration-(--md-sys-motion-spatial-fast-duration) ease-spatial-fast group-open/collapse:rotate-180" />
</summary>
<div @class(['pb-4 type-body-md text-on-surface-variant', 'px-4' => $variant === 'filled', 'pt-1'])>
{{ $slot }}
</div>
</details>