Skip to content

Commit

Permalink
Add example that separates label and input
Browse files Browse the repository at this point in the history
This is quite an advanced use which will stretch the Design System to
the limit. HTML has long-supported placing labels far from the
corresponding input, even outside the form altogether. The risk here is
that error messages now will be placed by the input, which has a strong
likelihood of breaking layouts.

Still, I'm sure some people will find a use for this.

Fixes #477
  • Loading branch information
peteryates committed Jul 21, 2024
1 parent d5c894f commit 58dd55e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions guide/content/introduction/labels-captions-hints-and-legends.slim
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ dl.govuk-summary-list
can enforce consistency between the label's <code>for</code> attribute
and the associated form element.

== render('/partials/example.*',
caption: 'Separate labels',
code: text_field_with_standalone_label,
start_headings_at_level: 3) do

markdown:
There are occasions where placing the label above an input won't work. We can
do this by suppressing the default label with `label: nil` and manually adding
the label elsewhere by calling the `govuk_label` helper directly.

hr.govuk-section-break.govuk-section-break--xl.govuk-section-break--visible

h2.govuk-heading-l Captions
Expand Down
4 changes: 4 additions & 0 deletions guide/content/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ $govuk-page-width: 1100px;
}
}

.two-columns {
columns: 2;
}

.branded-background {
background: govuk-colour(blue);
}
8 changes: 8 additions & 0 deletions guide/lib/examples/labels_captions_hints_and_legends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def text_field_with_label_proc
SNIPPET
end

def text_field_with_standalone_label
<<~SNIPPET
.two-columns
= f.govuk_label :crayons_or_felt_tips, text: "Do you prefer crayons or felt tip pens?"
= f.govuk_text_field :crayons_or_felt_tips, label: nil
SNIPPET
end

def text_field_with_caption
<<~SNIPPET
= f.govuk_text_field :favourite_shade_of_grey,
Expand Down
3 changes: 2 additions & 1 deletion guide/lib/helpers/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class Person
:favourite_shade_of_purple,
:favourite_shade_of_grey,
:favourite_primary_colour,
:least_favourite_colour
:least_favourite_colour,
:crayons_or_felt_tips
)

# errors
Expand Down

0 comments on commit 58dd55e

Please sign in to comment.