-
-
Notifications
You must be signed in to change notification settings - Fork 26
feat(components): add Kbd component #251
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
04b7b5a
feat: added initial component
Denny09310 fd0e813
feat: added kbd styling
Denny09310 d33e1de
fix: adding missing content slot
Denny09310 709a326
feat: added keyboard keys enum, added keys property
Denny09310 2753551
docs: added documentation page
Denny09310 2c44d44
feat: added rendering tests
Denny09310 2035044
fix: typed names of slots
Denny09310 df2004c
fix: missing XML element
Denny09310 fde46b5
feat: added Fn (fn), Win (Win) and Alt (⎇) symbols, moved KeyboardKye…
Denny09310 c507fce
chore: formatted razor file, changed some XML documentation
Denny09310 2883985
fix: added guard for unmapped values
Denny09310 6878d46
chore(*): minor adjustments
desmondinho 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
5 changes: 5 additions & 0 deletions
5
docs/LumexUI.Docs.Client/Pages/Components/Kbd/Examples/Keys.razor
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,5 @@ | ||
| <div class="flex items-center gap-2"> | ||
| <LumexKbd Keys="@([KeyboardKey.Command])">K</LumexKbd> | ||
| <LumexKbd Keys="@([KeyboardKey.Command, KeyboardKey.Shift])">N</LumexKbd> | ||
| <LumexKbd Keys="@([KeyboardKey.Option, KeyboardKey.Command])">P</LumexKbd> | ||
| </div> |
1 change: 1 addition & 0 deletions
1
docs/LumexUI.Docs.Client/Pages/Components/Kbd/Examples/Usage.razor
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 @@ | ||
| <LumexKbd Keys="@([KeyboardKey.Command])">K</LumexKbd> |
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,61 @@ | ||
| @page "/docs/components/kbd" | ||
| @layout DocsContentLayout | ||
|
|
||
| @using LumexUI.Docs.Client.Pages.Components.Kbd.PreviewCodes | ||
|
|
||
| <DocsSection Title="Usage"> | ||
| <Usage /> | ||
|
|
||
| <DocsSection Title="Keys"> | ||
| <p>Use the <code>Keys</code> parameter to specify a single keyboard key or a combination of keys.</p> | ||
| <Keys /> | ||
| </DocsSection> | ||
| </DocsSection> | ||
|
|
||
| <DocsSlotsSection Slots="@_slots"> | ||
| <div> | ||
| <h4 class="font-semibold">Kbd</h4> | ||
| <ul> | ||
| <li><code>Class</code>: The basic CSS class name styles the wrapper of the kbd contents.</li> | ||
| <li><code>Classes</code>: The CSS class names for the kbd slots style the entire kbd at once.</li> | ||
| </ul> | ||
| </div> | ||
| </DocsSlotsSection> | ||
|
|
||
| <DocsApiSection Components="@_apiComponents" /> | ||
|
|
||
| @code { | ||
| [CascadingParameter] private DocsContentLayout Layout { get; set; } = default!; | ||
|
|
||
| private readonly Heading[] _headings = new Heading[] | ||
| { | ||
| new("Usage", [ | ||
| new("Keys") | ||
| ]), | ||
| new("Custom Styles"), | ||
| new("API") | ||
| }; | ||
|
|
||
| private readonly Slot[] _slots = new Slot[] | ||
| { | ||
| new(nameof( KbdSlots.Base ), "Kbd wrapper, it handles alignment, placement, and general appearance."), | ||
| new(nameof( KbdSlots.Abbr ), "The keys wrapper that handles the appearance of the keys."), | ||
| new(nameof( KbdSlots.Content ), "The children wrapper that handles the appearance of the content."), | ||
| }; | ||
|
|
||
| private readonly string[] _apiComponents = new string[] | ||
| { | ||
| nameof( LumexKbd ) | ||
| }; | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| Layout.Initialize( | ||
| title: "Keyboard Key", | ||
| category: "Components", | ||
| description: "Keyboard key component for displaying keyboard shortcuts and input combinations.", | ||
| headings: _headings, | ||
| linksProps: new ComponentLinksProps( "Kbd", isServer: true ) | ||
| ); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
docs/LumexUI.Docs.Client/Pages/Components/Kbd/PreviewCodes/Keys.razor
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,5 @@ | ||
| @rendermode InteractiveWebAssembly | ||
|
|
||
| <PreviewCode Code="@new(name: null, snippet: "Kbd.Code.Keys")"> | ||
| <LumexUI.Docs.Client.Pages.Components.Kbd.Examples.Keys /> | ||
| </PreviewCode> |
5 changes: 5 additions & 0 deletions
5
docs/LumexUI.Docs.Client/Pages/Components/Kbd/PreviewCodes/Usage.razor
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,5 @@ | ||
| @rendermode InteractiveWebAssembly | ||
|
|
||
| <PreviewCode Code="@new(name: null, snippet: "Kbd.Code.Usage")"> | ||
| <LumexUI.Docs.Client.Pages.Components.Kbd.Examples.Usage /> | ||
| </PreviewCode> |
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,121 @@ | ||
| // Copyright (c) LumexUI 2024 | ||
| // LumexUI licenses this file to you under the MIT license | ||
| // See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
|
||
| namespace LumexUI.Common; | ||
|
|
||
| /// <summary> | ||
| /// Represents common keyboard keys for UI interactions. | ||
| /// </summary> | ||
| public enum KeyboardKey | ||
| { | ||
| /// <summary> | ||
| /// The Command key (⌘), commonly found on Apple keyboards. | ||
| /// </summary> | ||
| Command, | ||
|
|
||
| /// <summary> | ||
| /// The Shift key. | ||
| /// </summary> | ||
| Shift, | ||
|
|
||
| /// <summary> | ||
| /// The Control key (Ctrl). | ||
| /// </summary> | ||
| Control, | ||
|
|
||
| /// <summary> | ||
| /// The Option key (⌥), commonly found on Apple keyboards. | ||
| /// </summary> | ||
| Option, | ||
|
|
||
| /// <summary> | ||
| /// The Alt key (⎇), commonly found on Windows and Linux keyboards. | ||
| /// </summary> | ||
| Alt, | ||
|
|
||
| /// <summary> | ||
| /// The Windows key (⊞). | ||
| /// </summary> | ||
| Win, | ||
|
|
||
| /// <summary> | ||
| /// The Function (fn) key, often found on laptops. | ||
| /// </summary> | ||
| Fn, | ||
|
|
||
| /// <summary> | ||
| /// The Enter key. | ||
| /// </summary> | ||
| Enter, | ||
|
|
||
| /// <summary> | ||
| /// The Delete key. | ||
| /// </summary> | ||
| Delete, | ||
|
|
||
| /// <summary> | ||
| /// The Escape key (Esc). | ||
| /// </summary> | ||
| Escape, | ||
|
|
||
| /// <summary> | ||
| /// The Tab key. | ||
| /// </summary> | ||
| Tab, | ||
|
|
||
| /// <summary> | ||
| /// The Caps Lock key. | ||
| /// </summary> | ||
| CapsLock, | ||
|
|
||
| /// <summary> | ||
| /// The Up Arrow key. | ||
| /// </summary> | ||
| Up, | ||
|
|
||
| /// <summary> | ||
| /// The Right Arrow key. | ||
| /// </summary> | ||
| Right, | ||
|
|
||
| /// <summary> | ||
| /// The Down Arrow key. | ||
| /// </summary> | ||
| Down, | ||
|
|
||
| /// <summary> | ||
| /// The Left Arrow key. | ||
| /// </summary> | ||
| Left, | ||
|
|
||
| /// <summary> | ||
| /// The Page Up key. | ||
| /// </summary> | ||
| PageUp, | ||
|
|
||
| /// <summary> | ||
| /// The Page Down key. | ||
| /// </summary> | ||
| PageDown, | ||
|
|
||
| /// <summary> | ||
| /// The Home key. | ||
| /// </summary> | ||
| Home, | ||
|
|
||
| /// <summary> | ||
| /// The End key. | ||
| /// </summary> | ||
| End, | ||
|
|
||
| /// <summary> | ||
| /// The Help key. | ||
| /// </summary> | ||
| Help, | ||
|
|
||
| /// <summary> | ||
| /// The Space key (Spacebar). | ||
| /// </summary> | ||
| Space, | ||
| } | ||
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,36 @@ | ||
| // Copyright (c) LumexUI 2024 | ||
| // LumexUI licenses this file to you under the MIT license | ||
| // See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
|
||
| using LumexUI.Common; | ||
|
|
||
| namespace LumexUI; | ||
|
|
||
| internal static class KbdConstants | ||
| { | ||
| public static readonly Dictionary<KeyboardKey, string> KeyboardKeys = new() | ||
| { | ||
| [KeyboardKey.Command] = "⌘", | ||
| [KeyboardKey.Shift] = "⇧", | ||
| [KeyboardKey.Control] = "⌃", | ||
| [KeyboardKey.Option] = "⌥", | ||
| [KeyboardKey.Alt] = "⎇", | ||
| [KeyboardKey.Win] = "⊞", | ||
| [KeyboardKey.Fn] = "fn", | ||
| [KeyboardKey.Enter] = "↵", | ||
| [KeyboardKey.Delete] = "⌫", | ||
| [KeyboardKey.Escape] = "⎋", | ||
| [KeyboardKey.Tab] = "⇥", | ||
| [KeyboardKey.CapsLock] = "⇪", | ||
| [KeyboardKey.Up] = "↑", | ||
| [KeyboardKey.Right] = "→", | ||
| [KeyboardKey.Down] = "↓", | ||
| [KeyboardKey.Left] = "←", | ||
| [KeyboardKey.PageUp] = "⇞", | ||
| [KeyboardKey.PageDown] = "⇟", | ||
| [KeyboardKey.Home] = "↖", | ||
| [KeyboardKey.End] = "↘", | ||
| [KeyboardKey.Help] = "?", | ||
| [KeyboardKey.Space] = "␣", | ||
| }; | ||
| } |
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,26 @@ | ||
| // Copyright (c) LumexUI 2024 | ||
| // LumexUI licenses this file to you under the MIT license | ||
| // See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| using LumexUI.Common; | ||
|
|
||
| namespace LumexUI; | ||
|
|
||
| /// <summary> | ||
| /// Represents the set of customizable slots for the <see cref="LumexKbd"/> component. | ||
| /// </summary> | ||
| [ExcludeFromCodeCoverage] | ||
| public class KbdSlots : SlotBase | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the CSS class for the abbr slot. | ||
| /// </summary> | ||
| public string? Abbr { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the CSS class for the content slot. | ||
| /// </summary> | ||
| public string? Content { get; set; } | ||
| } |
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,22 @@ | ||
| @namespace LumexUI | ||
| @inherits LumexComponentBase | ||
|
|
||
| @using S = KbdSlots; | ||
| @using C = KbdConstants; | ||
|
|
||
| <LumexComponent As="@As" | ||
| Class="@GetStyles( nameof( S.Base ) )" | ||
| Style="@RootStyle" | ||
| data-slot="base" | ||
| @attributes="@AdditionalAttributes"> | ||
| @foreach( var key in Keys ) | ||
| { | ||
| <abbr class="@GetStyles( nameof( S.Abbr ) )" title="@key" data-slot="abbr"> | ||
| @( C.KeyboardKeys.TryGetValue( key, out var symbol ) ? symbol : null ) | ||
| </abbr> | ||
| } | ||
|
|
||
| <span class="@GetStyles( nameof( S.Content ) )" data-slot="content"> | ||
| @ChildContent | ||
| </span> | ||
| </LumexComponent> |
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,67 @@ | ||
| // Copyright (c) LumexUI 2024 | ||
| // LumexUI licenses this file to you under the MIT license | ||
| // See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| using LumexUI.Common; | ||
| using LumexUI.Utilities; | ||
|
|
||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace LumexUI; | ||
|
|
||
| /// <summary> | ||
| /// A component that represents a keyboard key or combination of keys used to perform a specific action. | ||
| /// </summary> | ||
| public partial class LumexKbd : LumexComponentBase, ISlotComponent<KbdSlots> | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the content to be rendered inside the component. | ||
| /// </summary> | ||
| [Parameter] public RenderFragment? ChildContent { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the collection of keys to be used as input for the component. | ||
| /// </summary> | ||
| [Parameter] public IEnumerable<KeyboardKey> Keys { get; set; } = []; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the CSS class names for the kbd slots. | ||
| /// </summary> | ||
| [Parameter] public KbdSlots? Classes { get; set; } | ||
|
|
||
| private Dictionary<string, ComponentSlot> _slots = []; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="LumexKbd"/>. | ||
| /// </summary> | ||
| public LumexKbd() | ||
| { | ||
| As = "kbd"; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void OnParametersSet() | ||
| { | ||
| var kbd = Styles.Kbd.Style( TwMerge ); | ||
| _slots = kbd(); | ||
| } | ||
|
|
||
| [ExcludeFromCodeCoverage] | ||
| private string? GetStyles( string slot ) | ||
| { | ||
| if( !_slots.TryGetValue( slot, out var styles ) ) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| return slot switch | ||
| { | ||
| nameof( KbdSlots.Base ) => styles( Classes?.Base, Class ), | ||
| nameof( KbdSlots.Abbr ) => styles( Classes?.Abbr ), | ||
| nameof( KbdSlots.Content ) => styles( Classes?.Content ), | ||
| _ => throw new NotImplementedException() | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Let's also add Fn (fn), Win (Win) and Alt (⎇).