Skip to content

Commit 6ad9d02

Browse files
author
Vanita Barrett
authored
Merge pull request #2367 from alphagov/add-negative-spacing
Allow `govuk-spacing` to output negative spacing
2 parents 23c6309 + 824c17f commit 6ad9d02

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ You can now use the `govuk-!-text-align-left`, `govuk-!-text-align-centre` and `
1010

1111
This was added in [pull request #2368: Add text align override classes](https://github.com/alphagov/govuk-frontend/pull/2368).
1212

13+
#### Define negative spacing using the `govuk-spacing` function
14+
15+
You can now pass the negative equivalent of a point from the spacing scale to the `govuk-spacing` function to get negative spacing.
16+
17+
For example, `govuk-spacing(1)` returns `5px`, and `govuk-spacing(-1)` returns `-5px`.
18+
19+
This was added in [pull request #2367: Allow govuk-spacing to output negative spacing](https://github.com/alphagov/govuk-frontend/pull/2367).
20+
1321
### Fixes
1422

1523
- [#2366: Fix panel text overflowing when zoomed in on mobile #2366](https://github.com/alphagov/govuk-frontend/pull/2366)

src/govuk/helpers/_spacing.scss

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@
88
///
99
/// Returns measurement corresponding to the spacing point requested.
1010
///
11-
/// @param {Number} $spacing-point - Point on the spacing scale (set in `settings/_spacing.sccs`)
11+
/// @param {Number} $spacing-point - Point on the spacing scale
12+
/// (set in `settings/_spacing.scss`)
1213
///
13-
/// @returns {String} Spacing Measurement eg. 10px
14+
/// @returns {String} Spacing measurement eg. 10px
1415
///
1516
/// @example scss
1617
/// .element {
1718
/// padding: govuk-spacing(5);
18-
/// top: govuk-spacing(2) !important; // if `!important` is required
1919
/// }
20+
///
21+
/// @example scss Using negative spacing
22+
/// .element {
23+
/// margin-top: govuk-spacing(-1);
24+
/// }
25+
///
26+
/// @example scss Marking spacing declarations as important
27+
/// .element {
28+
/// margin-top: govuk-spacing(1) !important;
29+
/// }
30+
///
2031
/// @access public
2132

2233
@function govuk-spacing($spacing-point) {
@@ -27,11 +38,18 @@
2738
+ "#{$actual-input-type}.";
2839
}
2940

41+
$is-negative: false;
42+
@if ($spacing-point < 0) {
43+
$is-negative: true;
44+
$spacing-point: abs($spacing-point);
45+
}
46+
3047
@if not map-has-key($govuk-spacing-points, $spacing-point) {
3148
@error "Unknown spacing variable `#{$spacing-point}`. Make sure you are using a point from the spacing scale in `_settings/spacing.scss`.";
3249
}
3350

34-
@return map-get($govuk-spacing-points, $spacing-point);
51+
$value: map-get($govuk-spacing-points, $spacing-point);
52+
@return if($is-negative, $value * -1, $value);
3553
}
3654

3755
/// Responsive spacing

src/govuk/helpers/spacing.test.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const sassBootstrap = `
2121
2222
// Emulates data from _settings/spacing.scss
2323
$govuk-spacing-points: (
24+
0: 0,
2425
2: 15px
2526
);
2627
@@ -52,7 +53,22 @@ describe('@function govuk-spacing', () => {
5253
top: 15px; }`)
5354
})
5455

55-
it('throws an exception when passed anything other than a number', async () => {
56+
it('returns CSS for a property based on a negative spacing point', async () => {
57+
const sass = `
58+
${sassBootstrap}
59+
60+
.foo {
61+
top: govuk-spacing(-2)
62+
}`
63+
64+
const results = await renderSass({ data: sass, ...sassConfig })
65+
66+
expect(results.css.toString().trim()).toBe(outdent`
67+
.foo {
68+
top: -15px; }`)
69+
})
70+
71+
it('throws an error when passed anything other than a number', async () => {
5672
const sass = `
5773
${sassBootstrap}
5874
@@ -66,6 +82,51 @@ describe('@function govuk-spacing', () => {
6682
'Expected a number (integer), but got a string.'
6783
)
6884
})
85+
86+
it('throws an error when passed a non-existent point', async () => {
87+
const sass = `
88+
${sassBootstrap}
89+
90+
.foo {
91+
top: govuk-spacing(999)
92+
}`
93+
94+
await expect(renderSass({ data: sass, ...sassConfig }))
95+
.rejects
96+
.toThrow(
97+
'Unknown spacing variable `999`. Make sure you are using a point from the spacing scale in `_settings/spacing.scss`.'
98+
)
99+
})
100+
101+
it('throws an error when passed a non-existent negative point', async () => {
102+
const sass = `
103+
${sassBootstrap}
104+
105+
.foo {
106+
top: govuk-spacing(-999)
107+
}`
108+
109+
await expect(renderSass({ data: sass, ...sassConfig }))
110+
.rejects
111+
.toThrow(
112+
'Unknown spacing variable `999`. Make sure you are using a point from the spacing scale in `_settings/spacing.scss`.'
113+
)
114+
})
115+
116+
it('handles negative zero', async () => {
117+
const sass = `
118+
${sassBootstrap}
119+
120+
.foo {
121+
top: govuk-spacing(-0)
122+
}`
123+
124+
const results = await renderSass({ data: sass, ...sassConfig })
125+
126+
expect(results.css.toString().trim()).toBe(outdent`
127+
.foo {
128+
top: 0; }`)
129+
})
69130
})
70131

71132
describe('@mixin _govuk-responsive-spacing', () => {

0 commit comments

Comments
 (0)