Skip to content

Commit

Permalink
feat(alert): futher work on alert template
Browse files Browse the repository at this point in the history
Handle bad params passed to the component - such as incorrect heading level, or heading with no
content
  • Loading branch information
chrispymm committed Feb 6, 2025
1 parent b8f4fe9 commit 51baf68
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
64 changes: 58 additions & 6 deletions docs/examples/alerts/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ figma_link: https://www.figma.com/file/N2xqOFkyehXwcD9DxU1gEq/MoJ-Figma-Kit?type
}) }}

{{ mojAlert({
type: "succcess",
type: "success",
heading: "Woohoo!",
text: "That thing just successfully occurred."
}) }}
Expand All @@ -58,7 +58,7 @@ heading: "Woah, hold up!",
text: "Bad things happened."
}) }}

<h2>with headings level</h2>
<h2>with headings level 3</h2>

{{ mojAlert({
type: "information",
Expand All @@ -67,6 +67,26 @@ text: "Bad things happened."
text: "Content that informs you of a thing."
}) }}

<h2>attempt to pass just a heading</h2>

{{ mojAlert({
type: "information",
heading: "This contains information",
dismissible: true
})
}}

<h2>attempt to use h1</h2>

{{ mojAlert({
type: "information",
heading: "This contains information",
headingLevel: 1,
dismissible: true,
text: "Content that informs you of a thing. It really is so very informative, that's why it needs so much content and is really, exceedingly verbose."
})
}}

<h2>Dismissible</h2>

{{ mojAlert({
Expand Down Expand Up @@ -108,10 +128,42 @@ dismissible: "yes",
})
}}


<h2>with headings and links</h2>

{{ mojAlert({
type: "information",
heading: "You might like to know",
dismissible: true,
html: 'Content that informs you of a thing. <a href="#">More information</a>'
}) }}

{{ mojAlert({
type: "success",
heading: "Woohoo!",
dismissible: true,
html: 'That thing just successfully occurred. <a href="#">Celebrate here</a>'
}) }}


{{ mojAlert({
type: "warning",
heading: "Something's not quite right",
dismissible: true,
html: 'You should be aware of this thing. <a href="#">More information</a>'
}) }}

{{ mojAlert({
type: "error",
heading: "Woah, hold up!",
dismissible: true,
html: 'Bad things happened. <a href="#">Contact us</a>'
}) }}

<h2>Kitchen sink</h2>
{{ mojAlert({
type: "information",
heading: "This contains information",
dismissible: true
})
}}

dismissible: true,
html: '<p>Content that informs you of a thing. It really is so very informative, that is why it needs so much content and is really, exceedingly verbose.</p><ul><li>this one</li><li>has a list</li><li>of things</li></ul><p>And a final paragraph for good measure <a href="#">with a link too</a>.'
}) }}
4 changes: 2 additions & 2 deletions gulp/dist-scss/all.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$_govuk-imported-modules: ("govuk/objects/width-container") !default;

@import "node_modules/govuk-frontend/dist/govuk/base";
@import "node_modules/govuk-frontend/dist/govuk/objects/width-container";
@import "govuk-frontend/dist/govuk/base";
@import "govuk-frontend/dist/govuk/objects/width-container";
@import "package/moj/all";
13 changes: 8 additions & 5 deletions src/moj/components/alert/_alert.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ $moj-colour-orange: #eb611c;
.moj-alert__content {
@include govuk-font($size: 19);
display: block;
padding-top: 5px;
overflow: hidden;
color: govuk-colour("black");

@include govuk-media-query($from: tablet) {
padding-top: 2px;
}
}

.moj-alert__action {
Expand All @@ -54,16 +59,14 @@ $moj-colour-orange: #eb611c;
line-height: 30px;
}

.moj-alert__content:not(:has(:is(h2, h3, h4))) {
padding-top: 5px;
.moj-alert--with-heading .moj-alert__content {
padding-top: 0;

@include govuk-media-query($from: tablet) {
padding-top: 2px;
padding-top: 0;
}
}

/* .moj-alert__content h2:last-child, */

.moj-alert__content *:last-child {
margin-bottom: 0;
}
Expand Down
20 changes: 13 additions & 7 deletions src/moj/components/alert/template.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{% from "govuk/macros/attributes.njk" import govukAttributes %}

{%- set headingLevel = params.headingLevel if params.headingLevel | int > 1 else 2 %}
{%- set content = params.html | safe if params.html else params.text %}

{#- Set classes for this component #}
{%- set classNames = ["moj-alert"] -%}

Expand All @@ -12,28 +15,31 @@
{%- set iconPath = '<path d="M11.2869 24.6726L2.00415 15.3899L4.62189 12.7722L11.2869 19.4186L25.3781 5.32739L27.9958 7.96369L11.2869 24.6726Z" fill="currentColor"/>' %}
{%- elif params.type == 'warning' %}
{%- set classNames = classNames.concat("moj-alert--warning") %}
{%- set iconPath = '<path d="M1.25 26.1992H28.75L15 2.44922L1.25 26.1992Z" fill="currentColor"/><path fill-rule="evenodd" clip-rule="evenodd" d="M16.4697 9.49585H13.5107L13.7461 17.7678H16.2431L16.4697 9.49585ZM13.2764 21.1072C13.1787 21.3285 13.1299 21.5662 13.1299 21.8201C13.1299 22.0805 13.1787 22.3246 13.2764 22.5525C13.374 22.7804 13.5075 22.9789 13.6768 23.1482C13.8525 23.3175 14.0544 23.4509 14.2822 23.5486C14.5101 23.6462 14.7542 23.6951 15.0146 23.6951C15.2686 23.6951 15.5062 23.6462 15.7275 23.5486C15.9554 23.4509 16.154 23.3175 16.3232 23.1482C16.4925 22.9789 16.626 22.7804 16.7236 22.5525C16.8213 22.3246 16.8701 22.0805 16.8701 21.8201C16.8701 21.5662 16.8213 21.3285 16.7236 21.1072C16.626 20.8793 16.4925 20.6807 16.3232 20.5115C16.154 20.3422 15.9554 20.2087 15.7275 20.1111C15.5062 20.0134 15.2686 19.9646 15.0146 19.9646C14.7542 19.9646 14.5101 20.0134 14.2822 20.1111C14.0544 20.2087 13.8525 20.3422 13.6768 20.5115C13.5075 20.6807 13.374 20.8793 13.2764 21.1072Z" fill="white" />' %}
{%- set iconPath = '<path fill-rule="evenodd" clip-rule="evenodd" d="M15 2.44922L28.75 26.1992H1.25L15 2.44922ZM13.5107 9.49579H16.4697L16.2431 17.7678H13.7461L13.5107 9.49579ZM13.1299 21.82C13.1299 21.5661 13.1787 21.3285 13.2764 21.1071C13.374 20.8793 13.5075 20.6807 13.6768 20.5114C13.8525 20.3421 14.0544 20.2087 14.2822 20.111C14.5101 20.0134 14.7542 19.9645 15.0146 19.9645C15.2686 19.9645 15.5062 20.0134 15.7275 20.111C15.9554 20.2087 16.154 20.3421 16.3232 20.5114C16.4925 20.6807 16.626 20.8793 16.7236 21.1071C16.8213 21.3285 16.8701 21.5661 16.8701 21.82C16.8701 22.0804 16.8213 22.3246 16.7236 22.5524C16.626 22.7803 16.4925 22.9789 16.3232 23.1481C16.154 23.3174 15.9554 23.4509 15.7275 23.5485C15.5062 23.6462 15.2686 23.695 15.0146 23.695C14.7542 23.695 14.5101 23.6462 14.2822 23.5485C14.0544 23.4509 13.8525 23.3174 13.6768 23.1481C13.5075 22.9789 13.374 22.7803 13.2764 22.5524C13.1787 22.3246 13.1299 22.0804 13.1299 21.82Z" fill="currentColor"/>' %}
{%- elif params.type == 'error' %}
{%- set classNames = classNames.concat("moj-alert--error") %}
{%- set iconPath = '<path d="M20.1777 2.5L9.82232 2.5L2.49999 9.82233L2.49999 20.1777L9.82232 27.5L20.1777 27.5L27.5 20.1777L27.5 9.82233L20.1777 2.5Z" fill="currentColor"/><path fill-rule="evenodd" clip-rule="evenodd" d="M16.4697 7.90039L13.5107 7.90039L13.7461 16.1724L16.2431 16.1724L16.4697 7.90039ZM13.2764 19.5117C13.1787 19.7331 13.1299 19.9707 13.1299 20.2246C13.1299 20.485 13.1787 20.7292 13.2764 20.957C13.374 21.1849 13.5075 21.3835 13.6768 21.5527C13.8525 21.722 14.0544 21.8555 14.2822 21.9531C14.5101 22.0508 14.7542 22.0996 15.0146 22.0996C15.2686 22.0996 15.5062 22.0508 15.7275 21.9531C15.9554 21.8555 16.154 21.722 16.3232 21.5527C16.4925 21.3835 16.626 21.1849 16.7236 20.957C16.8213 20.7292 16.8701 20.485 16.8701 20.2246C16.8701 19.9707 16.8213 19.7331 16.7236 19.5117C16.626 19.2839 16.4925 19.0853 16.3232 18.916C16.154 18.7467 15.9554 18.6133 15.7275 18.5156C15.5062 18.418 15.2686 18.3691 15.0146 18.3691C14.7542 18.3691 14.5101 18.418 14.2822 18.5156C14.0544 18.6133 13.8525 18.7467 13.6768 18.916C13.5075 19.0853 13.374 19.2839 13.2764 19.5117Z" fill="white"/>' %}
{%- set iconPath = '<path fill-rule="evenodd" clip-rule="evenodd" d="M9.82233 2.5H20.1777L27.5 9.82233V20.1777L20.1777 27.5H9.82233L2.5 20.1777V9.82233L9.82233 2.5ZM13.5107 7.90039L16.4697 7.90039L16.2431 16.1724L13.7461 16.1724L13.5107 7.90039ZM13.1299 20.2246C13.1299 19.9707 13.1787 19.7331 13.2764 19.5117C13.374 19.2839 13.5075 19.0853 13.6768 18.916C13.8525 18.7467 14.0544 18.6133 14.2822 18.5156C14.5101 18.418 14.7542 18.3691 15.0147 18.3691C15.2686 18.3691 15.5062 18.418 15.7275 18.5156C15.9554 18.6133 16.154 18.7467 16.3232 18.916C16.4925 19.0853 16.626 19.2839 16.7236 19.5117C16.8213 19.7331 16.8701 19.9707 16.8701 20.2246C16.8701 20.485 16.8213 20.7292 16.7236 20.957C16.626 21.1849 16.4925 21.3835 16.3232 21.5527C16.154 21.722 15.9554 21.8555 15.7275 21.9531C15.5062 22.0508 15.2686 22.0996 15.0147 22.0996C14.7542 22.0996 14.5101 22.0508 14.2822 21.9531C14.0544 21.8555 13.8525 21.722 13.6768 21.5527C13.5075 21.3835 13.374 21.1849 13.2764 20.957C13.1787 20.7292 13.1299 20.485 13.1299 20.2246Z" fill="currentColor"/>' %}
{%- else %}
{%- set classNames = classNames.concat("moj-alert--information") %}
{%- set iconPath = '<path d="M15 2.5C13.3585 2.5 11.733 2.82332 10.2165 3.45151C8.69989 4.07969 7.3219 5.00043 6.16117 6.16117C3.81696 8.50537 2.5 11.6848 2.5 15C2.5 18.3152 3.81696 21.4946 6.16117 23.8388C7.3219 24.9996 8.69989 25.9203 10.2165 26.5485C11.733 27.1767 13.3585 27.5 15 27.5C18.3152 27.5 21.4946 26.183 23.8388 23.8388C26.183 21.4946 27.5 18.3152 27.5 15C27.5 13.3585 27.1767 11.733 26.5485 10.2165C25.9203 8.69989 24.9996 7.3219 23.8388 6.16117C22.6781 5.00043 21.3001 4.07969 19.7835 3.45151C18.267 2.82332 16.6415 2.5 15 2.5Z" fill="currentColor"/><path fill-rule="evenodd" clip-rule="evenodd" d="M13.6621 22.4121H16.3574V12.95H13.6621V22.4121ZM13.5059 8.58398C13.4212 8.7793 13.3789 8.98763 13.3789 9.20898C13.3789 9.43685 13.4212 9.64844 13.5059 9.84375C13.5905 10.0391 13.7044 10.2116 13.8477 10.3613C13.9974 10.5046 14.1699 10.6185 14.3652 10.7031C14.5605 10.7878 14.7721 10.8301 15 10.8301C15.2214 10.8301 15.4297 10.7878 15.625 10.7031C15.8268 10.6185 15.9993 10.5046 16.1426 10.3613C16.2923 10.2116 16.4095 10.0391 16.4941 9.84375C16.5788 9.64844 16.6211 9.43685 16.6211 9.20898C16.6211 8.98763 16.5788 8.7793 16.4941 8.58398C16.4095 8.38216 16.2923 8.20964 16.1426 8.06641C15.9993 7.91667 15.8268 7.79948 15.625 7.71484C15.4297 7.63021 15.2214 7.58789 15 7.58789C14.7721 7.58789 14.5605 7.63021 14.3652 7.71484C14.1699 7.79948 13.9974 7.91667 13.8477 8.06641C13.7044 8.20964 13.5905 8.38216 13.5059 8.58398Z" fill="white"/>' %}
{%- set iconPath = '<path fill-rule="evenodd" clip-rule="evenodd" d="M10.2165 3.45151C11.733 2.82332 13.3585 2.5 15 2.5C16.6415 2.5 18.267 2.82332 19.7835 3.45151C21.3001 4.07969 22.6781 5.00043 23.8388 6.16117C24.9996 7.3219 25.9203 8.69989 26.5485 10.2165C27.1767 11.733 27.5 13.3585 27.5 15C27.5 18.3152 26.183 21.4946 23.8388 23.8388C21.4946 26.183 18.3152 27.5 15 27.5C13.3585 27.5 11.733 27.1767 10.2165 26.5485C8.69989 25.9203 7.3219 24.9996 6.16117 23.8388C3.81696 21.4946 2.5 18.3152 2.5 15C2.5 11.6848 3.81696 8.50537 6.16117 6.16117C7.3219 5.00043 8.69989 4.07969 10.2165 3.45151ZM16.3574 22.4121H13.6621V12.95H16.3574V22.4121ZM13.3789 9.20898C13.3789 8.98763 13.4212 8.7793 13.5059 8.58398C13.5905 8.38216 13.7044 8.20964 13.8477 8.06641C13.9974 7.91667 14.1699 7.79948 14.3652 7.71484C14.5605 7.63021 14.7721 7.58789 15 7.58789C15.2214 7.58789 15.4297 7.63021 15.625 7.71484C15.8268 7.79948 15.9993 7.91667 16.1426 8.06641C16.2923 8.20964 16.4095 8.38216 16.4941 8.58398C16.5788 8.7793 16.6211 8.98763 16.6211 9.20898C16.6211 9.43685 16.5788 9.64844 16.4941 9.84375C16.4095 10.0391 16.2923 10.2116 16.1426 10.3613C15.9993 10.5046 15.8268 10.6185 15.625 10.7031C15.4297 10.7878 15.2214 10.8301 15 10.8301C14.7721 10.8301 14.5605 10.7878 14.3652 10.7031C14.1699 10.6185 13.9974 10.5046 13.8477 10.3613C13.7044 10.2116 13.5905 10.0391 13.5059 9.84375C13.4212 9.64844 13.3789 9.43685 13.3789 9.20898Z" fill="currentColor"/>' %}

{%- endif %}

{%- if params.heading and content %}
{%- set classNames = classNames.concat("moj-alert--with-heading") %}
{%- endif %}

{%- if params.classes %}
{%- set classNames = classNames.concat(params.classes) %}
{%- endif %}

{%- set classNames = classNames | join(' ') -%}
{%- set headingLevel = params.headingLevel | default(2) %}
{%- set content = params.html | safe if params.html else params.text %}

<div
class="{{ classNames }}"
{{- govukAttributes(params.attributes) -}}
role="region"
aria-label="{{ params.iconFallbackText | default(params.type) | default("banner") }}"
aria-label="{{ params.type | default("information") }}"
>

<div class="moj-alert__icon">
Expand All @@ -42,7 +48,7 @@
</svg>
</div>

<div class="moj-alert__content">
<div class="moj-alert__content">
{%- if params.heading and content %}
<h{{-headingLevel }} class="govuk-heading-m">
{{-params.heading }}
Expand Down

0 comments on commit 51baf68

Please sign in to comment.