-
Notifications
You must be signed in to change notification settings - Fork 67
[Feature] Add Spinner component #521
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
tvq
wants to merge
3
commits into
ruby-ui:main
Choose a base branch
from
tvq:add_spinner
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,171 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Views::Docs::Spinner < Views::Base | ||
| def view_template | ||
| component = "Spinner" | ||
|
|
||
| div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do | ||
| render Docs::Header.new(title: "Spinner", description: "An indicator that can be used to show a loading state.") | ||
|
|
||
| Heading(level: 2) { "Usage" } | ||
|
|
||
| render Docs::VisualCodeExample.new(title: "Example", context: self) do | ||
| <<~RUBY | ||
| Spinner() | ||
| RUBY | ||
| end | ||
|
|
||
| render Docs::VisualCodeExample.new(title: "Size", description: "The size is controlled from the outside with any size utility.", context: self) do | ||
| <<~RUBY | ||
| div(class: "flex items-center gap-6") do | ||
| Spinner(class: "size-3") | ||
| Spinner(class: "size-4") | ||
| Spinner(class: "size-6") | ||
| Spinner(class: "size-8") | ||
| end | ||
| RUBY | ||
| end | ||
|
|
||
| render Docs::VisualCodeExample.new(title: "Color", description: "The spinner inherits the current text color.", context: self) do | ||
| <<~RUBY | ||
| div(class: "flex items-center gap-6") do | ||
| Spinner(class: "size-6 text-red-500") | ||
| Spinner(class: "size-6 text-green-500") | ||
| Spinner(class: "size-6 text-blue-500") | ||
| Spinner(class: "size-6 text-muted-foreground") | ||
| end | ||
| RUBY | ||
| end | ||
|
|
||
| render Docs::VisualCodeExample.new(title: "Button", description: "The button carries the state, the spinner is decorative.", context: self) do | ||
| <<~RUBY | ||
| div(class: "flex flex-wrap items-center gap-4") do | ||
| Button(disabled: true, aria: {busy: "true"}, class: "gap-2") do | ||
| Spinner(label: nil) | ||
| plain "Loading..." | ||
| end | ||
| Button(variant: :outline, disabled: true, aria: {busy: "true"}, class: "gap-2") do | ||
| Spinner(label: nil) | ||
| plain "Please wait" | ||
| end | ||
| Button(variant: :secondary, size: :sm, disabled: true, aria: {busy: "true"}, class: "gap-2") do | ||
| Spinner(label: nil, class: "size-3") | ||
| plain "Processing" | ||
| end | ||
| end | ||
| RUBY | ||
| end | ||
|
|
||
| render Docs::VisualCodeExample.new(title: "Badge", context: self) do | ||
| <<~RUBY | ||
| div(class: "flex flex-wrap items-center gap-4") do | ||
| Badge(class: "gap-1.5") do | ||
| Spinner(label: nil, class: "size-3") | ||
| plain "Syncing" | ||
| end | ||
| Badge(variant: :outline, class: "gap-1.5") do | ||
| Spinner(label: nil, class: "size-3") | ||
| plain "Updating" | ||
| end | ||
| Badge(variant: :destructive, class: "gap-1.5") do | ||
| Spinner(label: nil, class: "size-3") | ||
| plain "Deleting" | ||
| end | ||
| end | ||
| RUBY | ||
| end | ||
|
|
||
| render Docs::VisualCodeExample.new(title: "Empty", description: "Use a spinner as the media of an empty state to build a loading screen.", context: self) do | ||
| <<~RUBY | ||
| Empty(class: "w-full") do | ||
| EmptyHeader do | ||
| EmptyMedia(variant: :icon) do | ||
| Spinner() | ||
| end | ||
| EmptyTitle { "Processing your request" } | ||
| EmptyDescription { "Please wait while we process your request. Do not refresh the page." } | ||
| end | ||
| EmptyContent do | ||
| Button(variant: :outline, size: :sm) { "Cancel" } | ||
| end | ||
| end | ||
| RUBY | ||
| end | ||
|
|
||
| Heading(level: 2) { "Customization" } | ||
|
|
||
| Text do | ||
| plain "The generator copies " | ||
| InlineCode { "spinner.rb" } | ||
| plain " into " | ||
| InlineCode { "app/components/ruby_ui/spinner/" } | ||
| plain ", so the component belongs to your app. RubyUI inlines the SVG because the gem ships no icon dependency — but if your app has one, such as " | ||
| InlineLink(href: "https://github.com/AliOsm/phlex-icons", target: "_blank") { "phlex-icons" } | ||
| plain ", replace " | ||
| InlineCode { "view_template" } | ||
| plain " with the icon component and drop the two constants." | ||
| end | ||
|
|
||
| Codeblock(<<~RUBY, syntax: :ruby) | ||
| # app/components/ruby_ui/spinner/spinner.rb | ||
| module RubyUI | ||
| class Spinner < Base | ||
| include PhlexIcons | ||
|
|
||
| def view_template | ||
| Lucide::LoaderPinwheel(**attrs) | ||
| end | ||
|
|
||
| # initialize and default_attrs stay as they are; | ||
| # ICON_PATH and ICON_ATTRS are no longer needed | ||
| end | ||
| end | ||
| RUBY | ||
|
|
||
| Text do | ||
| plain "Without an icon gem, swap " | ||
| InlineCode { "ICON_PATH" } | ||
| plain " for the path data of any other 24x24 stroke icon. An icon drawn with more than one path needs those extra elements added to " | ||
| InlineCode { "view_template" } | ||
| plain " too." | ||
| end | ||
|
|
||
| Heading(level: 2) { "Accessibility" } | ||
|
|
||
| Text do | ||
| plain "A standalone spinner announces itself with " | ||
| InlineCode { 'role="status"' } | ||
| plain " and " | ||
| InlineCode { 'aria-label="Loading"' } | ||
| plain ". Pass " | ||
| InlineCode { "label:" } | ||
| plain " to describe the specific operation." | ||
| end | ||
|
|
||
| Text do | ||
| plain "Inside a button, badge or any other labelled control, pass " | ||
| InlineCode { "label: nil" } | ||
| plain " to render the spinner as decoration (" | ||
| InlineCode { 'aria-hidden="true"' } | ||
| plain ") and let the control announce the state with " | ||
| InlineCode { 'aria-busy="true"' } | ||
| plain ". A labelled spinner would otherwise be read out as part of the control's own name." | ||
| end | ||
|
|
||
| Codeblock(<<~RUBY, syntax: :ruby) | ||
| # announces itself | ||
| Spinner(label: "Saving changes") | ||
| # => <svg role="status" aria-label="Saving changes" ...> | ||
|
|
||
| # decorative, inside a control that announces the state | ||
| Spinner(label: nil) | ||
| # => <svg aria-hidden="true" ...> | ||
| RUBY | ||
|
|
||
| render Components::ComponentSetup::Tabs.new(component_name: component) | ||
|
|
||
| # components | ||
| render Docs::ComponentsTable.new(component_files(component)) | ||
| end | ||
| 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
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
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,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyUI | ||
| class Spinner < Base | ||
| # lucide loader-circle | ||
| ICON_PATH = "M21 12a9 9 0 1 1-6.219-8.56" | ||
|
|
||
| ICON_ATTRS = { | ||
| xmlns: "http://www.w3.org/2000/svg", | ||
| viewbox: "0 0 24 24", | ||
| fill: "none", | ||
| stroke: "currentColor", | ||
| stroke_width: "2", | ||
| stroke_linecap: "round", | ||
| stroke_linejoin: "round" | ||
| }.freeze | ||
|
|
||
| def initialize(label: "Loading", **attrs) | ||
| @label = label | ||
| super(**attrs) | ||
| end | ||
|
|
||
| def view_template | ||
| svg(**ICON_ATTRS, **attrs) do |s| | ||
| s.path(d: ICON_PATH) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def default_attrs | ||
| { | ||
| **announcement_attrs, | ||
| data: {slot: "spinner"}, | ||
| class: "size-4 shrink-0 animate-spin" | ||
| } | ||
| end | ||
|
|
||
| # A spinner inside a labelled control is decorative — the control announces busy. | ||
| def announcement_attrs | ||
| return {aria: {hidden: "true"}} if @label.to_s.strip.empty? | ||
|
|
||
| {role: "status", aria: {label: @label}} | ||
| end | ||
| end | ||
| end | ||
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.
Uh oh!
There was an error while loading. Please reload this page.