Textarea
Displays a textarea field.
Usage
Textarea
div(class: "grid w-full max-w-sm items-center gap-1.5") do
Textarea(placeholder: "Textarea")
end
Rows
div(class: "grid w-full max-w-sm items-center gap-1.5") do
Textarea(rows: 6, placeholder: "Fixed at 6 rows")
end
Disabled
div(class: "grid w-full max-w-sm items-center gap-1.5") do
Textarea(disabled: true, placeholder: "Disabled")
end
With formfield
This is a textarea
div(class: "grid w-full max-w-sm items-center gap-1.5") do
FormField do
FormFieldLabel(for: "textarea") { "Textarea" }
FormFieldHint { "This is a textarea" }
Textarea(placeholder: "Textarea", id: "textarea")
FormFieldError()
end
end
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component Textarea
Manual installation
1
Add RubyUI::Textarea to app/components/ruby_ui/textarea/textarea.rb
# frozen_string_literal: true
module RubyUI
class Textarea < Base
# Without rows the textarea grows with its content (field-sizing-content,
# min-h-16), as shadcn does. Passing rows keeps a fixed height
# (field-sizing-fixed), since field-sizing-content would ignore it.
def initialize(rows: nil, **attrs)
@rows = rows
@caller_slot = caller_slot?(attrs)
super(**attrs)
end
def view_template(&)
textarea(rows: @rows, **attrs, &)
end
private
# A caller's slot replaces "textarea" (shadcn spreads props after
# data-slot), so the default is left out instead of letting mix join both.
SLOT_ATTRS = [:data_slot, :"data-slot", "data-slot", "data_slot"].freeze
SLOT_KEYS = [:slot, :slot!, "slot", "slot!"].freeze
def caller_slot?(attrs)
return true if attrs.key?(:data!) || SLOT_ATTRS.any? { |key| attrs.key?(key) }
data = attrs[:data] || attrs["data"]
data.is_a?(Hash) && SLOT_KEYS.any? { |key| data.key?(key) }
end
def default_attrs
{
data: {
slot: (@caller_slot ? nil : "textarea"),
ruby_ui__form_field_target: "input",
action: "input->ruby-ui--form-field#onInput invalid->ruby-ui--form-field#onInvalid"
}.compact,
class: [
(@rows ? "field-sizing-fixed" : "field-sizing-content min-h-16"),
"flex w-full rounded-md border border-input bg-transparent px-2.5 py-2 text-base shadow-xs transition-[color,box-shadow] outline-none md:text-sm dark:bg-input/30",
"placeholder:text-muted-foreground",
"disabled:cursor-not-allowed disabled:opacity-50",
"focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50",
"aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40"
]
}
end
end
end
2
Add RubyUI::TextareaDocs to app/components/ruby_ui/textarea/textarea_docs.rb
# frozen_string_literal: true
class Views::Docs::Textarea < Views::Base
def view_template
component = "Textarea"
div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do
render Docs::Header.new(title: "Textarea", description: "Displays a textarea field.")
Heading(level: 2) { "Usage" }
render Docs::VisualCodeExample.new(title: "Textarea", context: self) do
<<~RUBY
div(class: "grid w-full max-w-sm items-center gap-1.5") do
Textarea(placeholder: "Textarea")
end
RUBY
end
render Docs::VisualCodeExample.new(title: "Rows", context: self) do
<<~RUBY
div(class: "grid w-full max-w-sm items-center gap-1.5") do
Textarea(rows: 6, placeholder: "Fixed at 6 rows")
end
RUBY
end
render Docs::VisualCodeExample.new(title: "Disabled", context: self) do
<<~RUBY
div(class: "grid w-full max-w-sm items-center gap-1.5") do
Textarea(disabled: true, placeholder: "Disabled")
end
RUBY
end
render Docs::VisualCodeExample.new(title: "With FormField", context: self) do
<<~RUBY
div(class: "grid w-full max-w-sm items-center gap-1.5") do
FormField do
FormFieldLabel(for: "textarea") { "Textarea" }
FormFieldHint { "This is a textarea" }
Textarea(placeholder: "Textarea", id: "textarea")
FormFieldError()
end
end
RUBY
end
end
render Components::ComponentSetup::Tabs.new(component_name: component)
render Docs::ComponentsTable.new(component_files(component))
end
end
Components
| Component | Built using | Source |
|---|---|---|
Textarea | Phlex | |
TextareaDocs | Phlex |