Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,31 @@ Mason::make('content')
->bricks([...])
```

### Grouping Bricks

Bricks can be organized into labeled groups in the editor sidebar by wrapping them in a `BrickGroup`. Groups are collapsible, and searching by brick name or tag will automatically expand any group that contains a match.

```php
use Awcodes\Mason\BrickGroup;

Mason::make('content')
->bricks([
BrickGroup::make('Content')
->bricks([
Section::class,
Grid::class,
]),
BrickGroup::make('Marketing')
->bricks([
Hero::class,
CallToAction::class,
]),
LeadForm::class, // standalone bricks can sit alongside groups
])
```

Groups and standalone bricks can be freely mixed in the same `bricks` array. `sortBricks()` applies to the top-level array and will sort groups alongside standalone bricks by their respective labels.

### Sorting Bricks

By default, bricks are sorted in the order they are defined in the `bricks` array. If you would like to allow users to sort the bricks in the editor, you can chain the `sortBricks` method on the field.
Expand Down
46 changes: 45 additions & 1 deletion resources/css/plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@
.mason-actions-bricks {
@apply flex flex-1 flex-col gap-2 overflow-y-auto px-3 py-2;

.mason-actions-group {
&.filtered {
display: none !important;
}
}

.mason-actions-group-header {
@apply w-full flex items-center justify-between py-2 text-xs font-semibold;

.mason-actions-group-chevron {
@apply size-3 transition-transform;
}
}

.mason-actions-group-bricks {
@apply flex flex-1 flex-col gap-2;
}

.mason-actions-brick {
@apply flex cursor-move items-center gap-2 rounded border border-gray-300 bg-white py-2 pe-4 ps-3 text-xs dark:border-gray-700 dark:bg-gray-800;

Expand All @@ -77,7 +95,15 @@
@apply w-full max-w-[13rem];

.mason-actions-bricks {
@apply grid flex-none auto-rows-fr grid-cols-1 md:grid-cols-2;
@apply grid flex-none grid-cols-1 md:grid-cols-2;

.mason-actions-group {
@apply col-span-full;

.mason-actions-group-bricks {
@apply grid grid-cols-1 md:grid-cols-2;
}
}

.mason-actions-brick {
@apply flex-col justify-center text-center;
Expand Down Expand Up @@ -179,6 +205,24 @@
@apply flex flex-1 flex-col gap-2 overflow-y-auto p-3;
}

.mason-brick-picker-group {
&.filtered {
display: none !important;
}
}

.mason-brick-picker-group-header {
@apply w-full flex items-center justify-between py-2 text-xs font-semibold text-gray-700 dark:text-gray-300;

.mason-brick-picker-group-chevron {
@apply size-3 transition-transform;
}
}

.mason-brick-picker-group-bricks {
@apply flex flex-col gap-2;
}

.mason-brick-picker-brick {
@apply flex cursor-pointer items-center gap-3 rounded-lg border border-gray-200 bg-white px-4 py-3 text-sm text-gray-700 transition hover:border-gray-300 hover:bg-gray-50 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-300 dark:hover:border-gray-600 dark:hover:bg-gray-700;

Expand Down
118 changes: 100 additions & 18 deletions resources/views/components/brick-picker-modal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@
])

@php
$brickData = array_map(fn ($brick) => ['label' => $brick::getLabel(), 'tags' => $brick::getTags()], $bricks);
$brickData = [];
foreach ($bricks as $item) {
if ($item instanceof \Awcodes\Mason\BrickGroup) {
foreach ($item->getBricks() as $brick) {
$brickData[] = ['label' => $brick::getLabel(), 'tags' => $brick::getTags(), 'group' => $item->getLabel()];
}
} else {
$brickData[] = ['label' => $item::getLabel(), 'tags' => $item::getTags(), 'group' => null];
}
}

$initialOpenGroups = [];
foreach ($bricks as $item) {
if ($item instanceof \Awcodes\Mason\BrickGroup) {
$initialOpenGroups[$item->getLabel()] = true;
}
}
@endphp

<div
Expand Down Expand Up @@ -31,6 +47,7 @@ class="mason-brick-picker-modal"
x-data="{
actions: @js($brickData),
search: '',
openGroups: @js($initialOpenGroups),
filterActions: function () {
const q = this.search.toLowerCase()
return this.actions
Expand All @@ -40,6 +57,23 @@ class="mason-brick-picker-modal"
)
.map((brick) => brick.label)
},
groupHasMatch: function (label) {
const q = this.search.toLowerCase()
if (! q) return true
return this.actions
.filter((b) => b.group === label)
.some((b) =>
b.label.toLowerCase().includes(q) ||
b.tags.some((t) => t.toLowerCase().includes(q)),
)
},
isGroupOpen: function (label) {
if (this.search) return true
return this.openGroups[label] !== false
},
toggleGroup: function (label) {
this.openGroups[label] = ! this.isGroupOpen(label)
},
}"
>
<div class="mason-brick-picker-header">
Expand Down Expand Up @@ -96,24 +130,72 @@ class="mason-brick-picker-position-btn"
</div>

<div class="mason-brick-picker-bricks">
@foreach ($bricks as $brick)
<button
type="button"
class="mason-brick-picker-brick"
x-on:click="insertFromPicker(@js($brick::getId()))"
x-bind:class="{
'filtered': ! filterActions().includes(@js($brick::getLabel())),
}"
>
@if (filled($brick::getIcon()))
<x-filament::icon
:icon="$brick::getIcon()"
class="h-5 w-5 shrink-0"
/>
@endif
@foreach ($bricks as $item)
@if ($item instanceof \Awcodes\Mason\BrickGroup)
<div
class="mason-brick-picker-group"
x-bind:class="{ 'filtered': ! groupHasMatch(@js($item->getLabel())) }"
>
<button
type="button"
class="mason-brick-picker-group-header"
x-on:click="toggleGroup(@js($item->getLabel()))"
>
<span class="mason-brick-picker-group-label">{{ $item->getLabel() }}</span>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="mason-brick-picker-group-chevron"
x-bind:class="{ 'rotate-180': isGroupOpen(@js($item->getLabel())) }"
>
<path fill-rule="evenodd" d="M5.22 8.22a.75.75 0 0 1 1.06 0L10 11.94l3.72-3.72a.75.75 0 1 1 1.06 1.06l-4.25 4.25a.75.75 0 0 1-1.06 0L5.22 9.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd" />
</svg>
</button>
<div
class="mason-brick-picker-group-bricks"
x-show="isGroupOpen(@js($item->getLabel()))"
>
@foreach ($item->getBricks() as $brick)
<button
type="button"
class="mason-brick-picker-brick"
x-on:click="insertFromPicker(@js($brick::getId()))"
x-bind:class="{
'filtered': ! filterActions().includes(@js($brick::getLabel())),
}"
>
@if (filled($brick::getIcon()))
<x-filament::icon
:icon="$brick::getIcon()"
class="h-5 w-5 shrink-0"
/>
@endif

<span>{{ $brick::getLabel() }}</span>
</button>
@endforeach
</div>
</div>
@else
<button
type="button"
class="mason-brick-picker-brick"
x-on:click="insertFromPicker(@js($item::getId()))"
x-bind:class="{
'filtered': ! filterActions().includes(@js($item::getLabel())),
}"
>
@if (filled($item::getIcon()))
<x-filament::icon
:icon="$item::getIcon()"
class="h-5 w-5 shrink-0"
/>
@endif

<span>{{ $brick::getLabel() }}</span>
</button>
<span>{{ $item::getLabel() }}</span>
</button>
@endif
@endforeach
</div>
</div>
Expand Down
Loading
Loading