Skip to content

Commit

Permalink
Merge pull request #61 from johntrickett86/add-keybindings
Browse files Browse the repository at this point in the history
Added support for registering keybindings to open the quick add dropdown component
  • Loading branch information
awcodes authored Nov 4, 2024
2 parents aa0051a + fd1d5b6 commit 5447c49
Show file tree
Hide file tree
Showing 4 changed files with 103 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
98 changes: 60 additions & 38 deletions resources/views/components/create-menu.blade.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
<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"
@if($keyBindings)
x-data="{
toggleDropdown() {
this.$refs.triggerButton.click()
this.$refs.triggerButton.focus()
}
}"
@endif
>
@if ($resources && $this->shouldBeHidden() === false)
<x-filament::dropdown placement="bottom-end" :teleport="true">
<x-slot name="trigger">
<button
x-ref="triggerButton"
@click="dropdownOpen = !dropdownOpen"
@if($keyBindings)
x-ref="triggerButton"
x-mousetrap.global.{{ $keyBindings }}="toggleDropdown"
@endif
@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>
13 changes: 12 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,7 @@ public function mount(): void
$this->rounded = QuickCreatePlugin::get()->isRounded();
$this->hiddenIcons = QuickCreatePlugin::get()->shouldHideIcons();
$this->label = QuickCreatePlugin::get()->getLabel();
$this->keyBindings = QuickCreatePlugin::get()->getKeyBindings();
}

/**
Expand Down Expand Up @@ -146,13 +149,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 5447c49

Please sign in to comment.