-
Notifications
You must be signed in to change notification settings - Fork 0
Add a command-list component #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
coorasse
wants to merge
1
commit into
main
Choose a base branch
from
command_list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| .command-list { | ||
| .command-list-button { | ||
| min-width: 170px; | ||
| } | ||
|
|
||
| .modal-dialog { | ||
| --bs-modal-width: 600px; | ||
| --bs-modal-margin: 5rem; | ||
| --bs-modal-padding: 1.5rem; | ||
| } | ||
|
|
||
| .list-group-item { | ||
| color: $navbar-light-color; | ||
|
|
||
| &.active { | ||
| color: white; | ||
| } | ||
| } | ||
|
|
||
| .command-list-footer { | ||
| color: $navbar-light-color; | ||
| } | ||
|
|
||
| .command-list-icon { | ||
| border: 1px outset $navbar-light-color; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # frozen_literal_string: true | ||
|
|
||
| class CommandListComponentPreview < Lookbook::Preview | ||
| include LookbookHelper | ||
|
|
||
| # @source ../../../app/views/components/command_lists/_themes.html.erb | ||
| def themes | ||
| render 'components/command_lists/themes' | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||
| import { Controller } from "@hotwired/stimulus" | ||||||
|
|
||||||
| export default class CommandListController extends Controller { | ||||||
| static targets = ['list', 'metaKey', "searchField", "listGroup", "listItem"]; | ||||||
|
|
||||||
| connect() { | ||||||
| this.modal = new bootstrap.Modal(this.listTarget, {}) | ||||||
| this.children = Array.from(this.listGroupTargets[0].children); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not
Suggested change
If it's just one target as far as I see |
||||||
| if (this.listItemTargets.length > 0) { | ||||||
| this.switchActive(this.listItemTargets[0]); | ||||||
| } | ||||||
| this.metaKeyTarget.innerText = this.getMetaKey() | ||||||
|
|
||||||
| document.addEventListener("keydown", this.handleKeydown.bind(this)); | ||||||
| } | ||||||
|
|
||||||
| disconnect() { | ||||||
| document.removeEventListener("keydown", this.handleKeydown.bind(this)); | ||||||
| } | ||||||
|
|
||||||
| handleKeydown(event) { | ||||||
| console.log('handling keydown', event.metaKey, event.key); | ||||||
| if ((event.metaKey) && event.key === "k") { | ||||||
| this.openModal(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| openModal() { | ||||||
| this.modal.show(); | ||||||
| this.searchField().focus(); | ||||||
| } | ||||||
|
|
||||||
| performCommand(event) { | ||||||
| event.preventDefault(); | ||||||
| const searchParts = this.searchField().value.split(',').map(part => part.trim()); | ||||||
| const command = this.activeCommand.dataset.command | ||||||
| .replace('_1_', searchParts[0]) | ||||||
| .replace('_2_', searchParts[1]) | ||||||
| .replace('_3_', searchParts[2]); | ||||||
| if (this.activeCommand.dataset.target == "_blank") { | ||||||
| window.open(command, '_blank'); | ||||||
| } else { | ||||||
| Turbo.visit(command); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| getMetaKey() { | ||||||
| const userAgent = window.navigator.userAgent.toLowerCase(); | ||||||
| if (userAgent.includes(' mac ')) { | ||||||
| return '⌘'; | ||||||
| } else { | ||||||
| return 'CTRL'; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| switchActive(element) { | ||||||
| if (this.activeCommand) { | ||||||
| this.activeCommand.classList.remove('active'); | ||||||
| } | ||||||
| element.classList.add('active'); | ||||||
| this.activeCommand = element; | ||||||
| this.searchField().placeholder = element.dataset.placeholder ?? ''; | ||||||
| } | ||||||
|
|
||||||
| selectCommand(event) { | ||||||
| event.preventDefault(); | ||||||
| this.switchActive(event.currentTarget); | ||||||
| this.searchField().focus() | ||||||
| } | ||||||
|
|
||||||
| previousItem() { | ||||||
| const previous = this.listItemTargets[this.listItemTargets.indexOf(this.activeCommand) - 1]; | ||||||
| if (previous) { | ||||||
| this.switchActive(previous) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| nextItem() { | ||||||
| const next = this.listItemTargets[this.listItemTargets.indexOf(this.activeCommand) + 1]; | ||||||
| if (next) { | ||||||
| this.switchActive(next) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| searchField() { | ||||||
| return this.searchFieldTarget | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <div class="alert alert-warning"> | ||
| Since this page is an iframe, you need to click here first, and then press the shortcut. | ||
| </div> | ||
| <% | ||
|
|
||
| command_list_actions = [ | ||
| { label: 'Search on Google', | ||
| command: 'https://www.google.com/search?q=_1_', | ||
| placeholder: 'Search text', | ||
| category: 'search', | ||
| target: '_blank' }, | ||
| { label: 'Go to component', | ||
| command: '/lookbook/inspect/_1_/themes', | ||
| placeholder: 'badge, button, avatar, etc.', | ||
| category: 'link' }, | ||
| ].group_by { |action| action[:category] } | ||
|
|
||
| %> | ||
| <div data-controller="command-list" class="command-list"> | ||
| <a href="#" class="nav-item me-3 p-1 text-decoration-none"> | ||
| <div class="input-group command-list-button ms-2" data-bs-toggle="modal" data-bs-target="#command-list-modal"> | ||
| <span class="input-group-text" id="basic-addon1"><i class="fa fa-search"></i></span> | ||
| <div class="form-control form-control-sm text-center py-2"> | ||
| <%= t('command_list.press') %> | ||
| <kbd class="command-list-icon" data-command-list-target="metaKey"></kbd> + <kbd class="command-list-icon">K</kbd> | ||
| </div> | ||
| </div> | ||
| </a> | ||
|
|
||
| <div id="command-list-modal" class="modal" tabindex="-1" data-command-list-target="list"> | ||
| <div class="modal-dialog"> | ||
| <div class="modal-content"> | ||
| <div class="modal-body"> | ||
| <div class="input-group mb-3"> | ||
| <span class="input-group-text" id="basic-addon1"><i class="fa fa-search"></i></span> | ||
| <input autocomplete="off" | ||
| type="text" | ||
| class="form-control" | ||
| data-command-list-target="searchField" | ||
| data-action="keydown.up->command-list#previousItem | ||
| keydown.enter->command-list#performCommand | ||
| keydown.down->command-list#nextItem"> | ||
| </div> | ||
|
|
||
| <div class="list-group" data-command-list-target="listGroup"> | ||
| <% command_list_actions.each do |category, actions| %> | ||
| <div class="form-label mt-2 fw-medium"><%= t("command_list.#{category}") %></div> | ||
| <% actions.each do |action| %> | ||
| <a class="list-group-item" href="#" | ||
| data-action="click->command-list#selectCommand" | ||
| data-command-list-target="listItem" | ||
| data-placeholder="<%= action[:placeholder] || '' %>" | ||
| data-command="<%= action[:command] %>" | ||
| data-target="<%= action[:target] || '' %>"> | ||
| <%= action[:label] %> | ||
| </a> | ||
| <% end %> | ||
| <% end %> | ||
| </div> | ||
|
|
||
| </div> | ||
| <div class="modal-footer d-flex command-list-footer"> | ||
| <small class="pe-1"><kbd class="command-list-icon">⏎</kbd> <%= t('command_list.to_select') %></small> | ||
| <small class="pe-1"> | ||
| <kbd class="command-list-icon">↑</kbd> | ||
| <kbd class="command-list-icon">↓</kbd> | ||
| <%= t('command_list.to_navigate') %> | ||
| </small> | ||
| <small class="pe-1"><kbd class="command-list-icon">ESC</kbd> <%= t('command_list.to_exit') %></small> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need so much custom css here? Also doesn't look good on mobile with so much margin.
I'd also have a look at the rest of this file's classes if we really need them. (some probably yes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also pretty sure there are provided classes for there, like
modal-sm