Skip to content
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

Allow creating custom width containers and using them with template #1626

Merged
merged 5 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,34 @@

### New features

#### Allow attributes to be set on `<body>` of template
#### Create custom width container classes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good work on the CHANGELOG, nice 👍


You can now create custom page width container classes using the `govuk-width-container` mixin. You do this by passing in the required maximum width of the container.

For example:

```scss
.app-width-container--wide {
@include govuk-width-container(1200px);
}
```

You can use the generated classes to set the width of:
- template container
- header container
- footer container

It was already possible to set the page app width with the `$govuk-page-width` variable. This new feature is useful when creating additional custom page width classes.

- [Pull request #1626: Allow creating custom width containers and using them with template](https://github.com/alphagov/govuk-frontend/pull/1626).

#### Set custom container classes on template

You can now set classes on `.govuk-width-container` in the template with `containerClasses`. This is useful if you want to set a custom width class on the template container.

- [Pull request #1626: Allow creating custom width containers and using them with template](https://github.com/alphagov/govuk-frontend/pull/1626).

#### Set attributes on the `<body>` of template

You can now set attributes in the `<body>` element of page template.

Expand Down
4 changes: 4 additions & 0 deletions app/assets/scss/partials/_app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
background-color: $govuk-body-background-colour;
}

.app-width-container--wide {
@include govuk-width-container(1200px);
}

.app-iframe-in-component-preview {
margin: 15px 0;
}
Expand Down
3 changes: 3 additions & 0 deletions app/views/examples/template-custom/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{% set assetPath = '' %}
{% set themeColor = 'blue' %}
{% set bodyClasses = 'app-body-class' %}
{% set containerClasses = "app-width-container--wide" %}

{% block pageTitle %}GOV.UK - Le meilleur endroit pour trouver des services gouvernementaux et de l'information{% endblock %}

Expand Down Expand Up @@ -54,6 +55,7 @@
<!-- block:header -->
{{ govukHeader({
serviceName: "Nom du service",
containerClasses: "app-width-container--wide",
navigation: [
{
href: '#1',
Expand Down Expand Up @@ -104,6 +106,7 @@
{% block footer %}
<!-- block:footer -->
{{ govukFooter({
containerClasses: "app-width-container--wide",
"meta": {
"items": [
{
Expand Down
28 changes: 23 additions & 5 deletions src/govuk/objects/_width-container.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
@import "../tools/all";
@import "../helpers/all";

@mixin govuk-width-container {
// Limit the width of the container to the page width
max-width: $govuk-page-width;
////
/// @group objects
////

/// Width container mixin
///
/// Used to create page width and custom width container classes.
///
/// @param {String} $width [$govuk-page-width] - Width in pixels
///
/// @example scss - Creating a 1200px wide container class
/// .app-width-container--wide {
/// @include govuk-width-container(1200px);
/// }
///
/// @access public

@mixin govuk-width-container($width: $govuk-page-width) {

// By default, limit the width of the container to the page width
max-width: $width;

// On mobile, add half width gutters
margin: 0 $govuk-gutter-half;
Expand Down Expand Up @@ -38,7 +56,7 @@

// As soon as the viewport is greater than the width of the page plus the
// gutters, just centre the content instead of adding gutters.
@include govuk-media-query($and: "(min-width: #{($govuk-page-width + $govuk-gutter * 2)})") {
@include govuk-media-query($and: "(min-width: #{($width + $govuk-gutter * 2)})") {
margin: 0 auto;

// Since a safe area may have previously been set above,
Expand All @@ -49,7 +67,7 @@
}

@include govuk-if-ie8 {
width: $govuk-page-width;
width: $width;
// Since media queries are not supported in IE8,
// we need to duplicate this margin that centers the page.
margin: 0 auto;
Expand Down
48 changes: 48 additions & 0 deletions src/govuk/objects/width-container.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env jest */

const outdent = require('outdent')

const { renderSass } = require('../../../lib/jest-helpers')

const sassConfig = {
outputStyle: 'nested'
}

describe('@mixin govuk-width-container', () => {
it('allows different widths to be specified using $width', async () => {
const sass = `
@import "objects/width-container";

.app-width-container--wide {
@include govuk-width-container(1200px);
}
`
const results = await renderSass({ data: sass, ...sassConfig })

expect(results.css
.toString()
.trim())
.toContain(outdent`
.app-width-container--wide {
max-width: 1200px;
margin: 0 15px; }
@supports (margin: max(calc(0px))) {
.app-width-container--wide {
margin-right: max(15px, calc(15px + env(safe-area-inset-right)));
margin-left: max(15px, calc(15px + env(safe-area-inset-left))); } }
@media (min-width: 40.0625em) {
.app-width-container--wide {
margin: 0 30px; }
@supports (margin: max(calc(0px))) {
.app-width-container--wide {
margin-right: max(30px, calc(15px + env(safe-area-inset-right)));
margin-left: max(30px, calc(15px + env(safe-area-inset-left))); } } }
@media (min-width: 1260px) {
.app-width-container--wide {
margin: 0 auto; }
@supports (margin: max(calc(0px))) {
.app-width-container--wide {
margin: 0 auto; } } }
`)
})
})
2 changes: 1 addition & 1 deletion src/govuk/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{% endblock %}

{% block main %}
<div class="govuk-width-container">
<div class="govuk-width-container {{ containerClasses }}">
hannalaakso marked this conversation as resolved.
Show resolved Hide resolved
{% block beforeContent %}{% endblock %}
<main class="govuk-main-wrapper {{ mainClasses }}" id="main-content" role="main"{% if mainLang %} lang="{{ mainLang }}"{% endif %}>
{% block content %}{% endblock %}
Expand Down