Skip to content

Commit

Permalink
Allow long option names to wrap at full stops and capital letters
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Aug 15, 2023
1 parent f452d01 commit 5cd1b9d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
39 changes: 30 additions & 9 deletions lib/get-macro-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ function addSlugs(option) {
return option
}

function renderNameWithBreaks(option) {
if (option.params) {
option.params = option.params.map(renderNameWithBreaks)
}

// Add suggested word breaks before capital letters to allow
// browsers to break long option names in sensible places
option.name = option.name.replace(/(?<!<wbr>)([A-Z])/g, '<wbr>$1')

// Also add suggested word breaks after full stops for options
// that are manually given parent prefixes like `summary.text`
option.name = option.name.replace(/(\.)(?!<wbr>)/g, '$1<wbr>')

return option
}

function renderDescriptionsAsMarkdown(option) {
if (option.description) {
option.description = marked(option.description, { renderer })
Expand Down Expand Up @@ -146,34 +162,39 @@ function getMacroOptions(componentName) {
componentName = 'input'
}

const options = getMacroOptionsJson(componentName)
.map(addSlugs)
// Macro options
const options = getMacroOptionsJson(componentName).map(addSlugs)

// Macro options with formatting
const optionsFormatted = structuredClone(options)
.map(renderNameWithBreaks)
.map(renderDescriptionsAsMarkdown)

const nestedOptions = getNestedOptions(options)
const nestedOptions = getNestedOptions(optionsFormatted)
const additionalComponents = getAdditionalComponentOptions(options)

const optionGroups = [
{
name: 'Primary options',
id: 'primary',
options
options: optionsFormatted
}
]
.concat(
nestedOptions.map((option) => ({
name: 'Options for ' + option.name,
name: `Options for ${option.name}`,
id: option.slug,
options: option.params
}))
)
.concat(
additionalComponents.map((option) => ({
name: 'Options for ' + option.name,
name: `Options for ${option.name}`,
id: option.slug,
options: getMacroOptionsJson(option.name).map(
renderDescriptionsAsMarkdown
)
options: getMacroOptionsJson(option.name)
.map(addSlugs)
.map(renderNameWithBreaks)
.map(renderDescriptionsAsMarkdown)
}))
)

Expand Down
5 changes: 4 additions & 1 deletion src/stylesheets/components/_options.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
.govuk-table__header,
.govuk-table__cell {
padding-right: govuk-spacing(2);
word-break: break-word;

// Automatic wrapping for unbreakable option names
word-wrap: break-word; // Fallback for older browsers only
overflow-wrap: break-word;
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions views/partials/_example.njk
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</p>
{% for table in macroOptions %}
<table class="govuk-table app-options__table" id="options-{{ exampleId }}--{{ table.id }}">
<caption class="govuk-table__caption govuk-heading-m {% if table.id == 'primary' %} govuk-visually-hidden{% endif %}">{{ table.name }}</caption>
<caption class="govuk-table__caption govuk-heading-m {% if table.id == 'primary' %} govuk-visually-hidden{% endif %}">{{ table.name | safe }}</caption>
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th class="govuk-table__header app-options__limit-table-cell" scope="col">Name</th>
Expand All @@ -105,23 +105,23 @@
<tbody class="govuk-table__body">
{% for option in table.options -%}
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="row">{{option.name}}</th>
<td class="govuk-table__cell ">{{option.type}}</td>
<td class="govuk-table__cell ">
<th class="govuk-table__header" scope="row">{{ option.name | safe }}</th>
<td class="govuk-table__cell">{{ option.type }}</td>
<td class="govuk-table__cell">
{% if (option.required === true) %}
<strong>Required.</strong>
{% endif %}
{{ option.description | safe }}
{% if (option.isComponent) -%}
{# Create separate table data for components that are hidden in the Design System -#}
{% if ["hint", "label"].includes(option.slug) %}
See <a href="#options-{{ exampleId }}--{{ option.slug }}">{{ option.name }}</a>.
See <a href="#options-{{ exampleId }}--{{ option.slug }}">{{ option.name | safe }}</a>.
{% else %}
See <a href="/components/{{ option.slug }}/#options-{{ option.slug }}-example">{{ option.name }}</a>.
See <a href="/components/{{ option.slug }}/#options-{{ option.slug }}-example">{{ option.name | safe }}</a>.
{% endif %}
{% endif %}
{% if (option.params) %}
See <a href="#options-{{ exampleId }}--{{ option.slug }}">{{ option.name }}</a>.
See <a href="#options-{{ exampleId }}--{{ option.slug }}">{{ option.name | safe }}</a>.
{% endif -%}
</td>
</tr>
Expand Down

0 comments on commit 5cd1b9d

Please sign in to comment.