Skip to content

Commit

Permalink
added support for keybindings to open quick add dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
johntrickett86 committed Nov 2, 2024
1 parent aa0051a commit 8c6f9e2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 41 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ public function panel(Panel $panel): Panel
}
```

### Registering keybindings

You can attach keyboard shortcuts to trigger the Quick Create dropdown. To configure these, pass the keyBindings() method to the configuration:

```php
use Awcodes\FilamentQuickCreate\QuickCreatePlugin;

public function panel(Panel $panel): Panel
{
return $panel
->plugins([
QuickCreatePlugin::make()
->keyBindings(['command+shift+a', 'ctrl+shift+a']),
])
}
```

### Appearance

#### Rounded
Expand Down
86 changes: 48 additions & 38 deletions resources/views/components/create-menu.blade.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
<div class="quick-create-component">
@if ($resources && $this->shouldBeHidden() === false)
<x-filament::dropdown placement="bottom-end" :teleport="true">
<x-slot name="trigger">
<button
@class([
'flex flex-shrink-0 bg-gray-100 items-center justify-center text-primary-500 hover:text-primary-900 dark:bg-gray-800 hover:bg-primary-500 dark:hover:bg-primary-500',
'rounded-full' => $rounded,
'rounded-md' => ! $rounded,
'w-8 h-8' => ! $label,
'py-1 ps-3 pe-4 gap-1' => $label,
])
aria-label="{{ __('filament-quick-create::quick-create.button_label') }}"
>
<x-filament::icon
alias="filament-quick-create::add"
icon="heroicon-o-plus"
class="w-5 h-5"
/>
@if ($label)
<span class="">{{ $label }}</span>
@endif
</button>
</x-slot>
<x-filament::dropdown.list>
@foreach($resources as $resource)
<x-filament::dropdown.list.item
:icon="$hiddenIcons ? null : $resource['icon']"
:wire:click="$resource['action']"
:href="$resource['url']"
:tag="$resource['url'] ? 'a' : 'button'"
@php
$keyBindings = collect($keyBindings ?? [])
->map(fn (string $keyBinding): string => str_replace('+', '-', $keyBinding))
->implode('.');
@endphp

<div class="quick-create-component" x-data="{ dropdownOpen: false }">
@if ($resources && $this->shouldBeHidden() === false)
<x-filament::dropdown placement="bottom-end" :teleport="true">
<x-slot name="trigger">
<button
x-ref="triggerButton"
@click="dropdownOpen = !dropdownOpen"
x-mousetrap.global.{{ $keyBindings }}="$refs.triggerButton.click()"
@class([
'flex flex-shrink-0 bg-gray-100 items-center justify-center text-primary-500 hover:text-primary-900 dark:bg-gray-800 hover:bg-primary-500 dark:hover:bg-primary-500',
'rounded-full' => $rounded,
'rounded-md' => ! $rounded,
'w-8 h-8' => ! $label,
'py-1 ps-3 pe-4 gap-1' => $label,
])
aria-label="{{ __('filament-quick-create::quick-create.button_label') }}"
>
{{ $resource['label'] }}
</x-filament::dropdown.list.item>
@endforeach
</x-filament::dropdown.list>
</x-filament::dropdown>
<x-filament::icon
alias="filament-quick-create::add"
icon="heroicon-o-plus"
class="w-5 h-5"
/>
@if ($label)
<span class="">{{ $label }}</span>
@endif
</button>
</x-slot>

<x-filament::dropdown.list>
@foreach($resources as $resource)
<x-filament::dropdown.list.item
:icon="$hiddenIcons ? null : $resource['icon']"
:wire:click="$resource['action']"
:href="$resource['url']"
:tag="$resource['url'] ? 'a' : 'button'"
>
{{ $resource['label'] }}
</x-filament::dropdown.list.item>
@endforeach
</x-filament::dropdown.list>
</x-filament::dropdown>

<x-filament-actions::modals />
@endif
<x-filament-actions::modals/>
@endif
</div>
15 changes: 14 additions & 1 deletion src/Components/QuickCreateMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class QuickCreateMenu extends Component implements HasActions, HasForms

public ?string $label = null;

public ?array $keyBindings = null;

/**
* @throws Exception
*/
Expand All @@ -42,6 +44,9 @@ public function mount(): void
$this->rounded = QuickCreatePlugin::get()->isRounded();
$this->hiddenIcons = QuickCreatePlugin::get()->shouldHideIcons();
$this->label = QuickCreatePlugin::get()->getLabel();

// Retrieve keybindings from the plugin
$this->keyBindings = QuickCreatePlugin::get()->getKeyBindings();
}

/**
Expand Down Expand Up @@ -146,13 +151,21 @@ public function getActions(): array
->toArray();
}

public function toggleDropdown(): void
{
$this->emit('toggleQuickCreateDropdown');
}

public function shouldBeHidden(): bool
{
return QuickCreatePlugin::get()->shouldBeHidden();
}

public function render(): View
{
return view('filament-quick-create::components.create-menu');
return view('filament-quick-create::components.create-menu')
->with([
'keyBindings' => $this->keyBindings,
]);
}
}
16 changes: 14 additions & 2 deletions src/QuickCreatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ class QuickCreatePlugin implements Plugin

protected bool | Closure $shouldUseModal = false;

protected string | array | Closure | null $keyBindings = null;

public function boot(Panel $panel): void
{
Livewire::component('quick-create-menu', Components\QuickCreateMenu::class);

$this->getResourcesUsing(fn () => $panel->getResources());
}

Expand Down Expand Up @@ -184,7 +185,6 @@ public function sortBy(string | Closure $sortBy = 'label'): static
if (! in_array($sortBy, ['label', 'navigation'])) {
$sortBy = 'label';
}

$this->sortBy = $sortBy;

return $this;
Expand Down Expand Up @@ -249,4 +249,16 @@ public function alwaysShowModal(bool | Closure $condition = true): static

return $this;
}

public function keyBindings(string | array | Closure | null $bindings): static
{
$this->keyBindings = $bindings;

return $this;
}

public function getKeyBindings(): ?array
{
return collect($this->evaluate($this->keyBindings))->toArray();
}
}

0 comments on commit 8c6f9e2

Please sign in to comment.