Skip to content

Commit add5491

Browse files
36degreesVanita Barrett
authored andcommitted
Allow govuk-spacing to output negative spacing
Occasionally, users need to use the `govuk-spacing` class to output negative spacing, for example to add a negative margin to bring elements closer together. There are currently a few ways to do this. You can use `- govuk-spacing(n)` but the extra space is required, and it looks like a calculation (and may be interpreted as such in some contexts). You can also multiply the result of the function call by -1, for example `govuk-spacing(n) * -1`, but this feels a little clunky. To make it easier for users to do this, allow govuk-spacing to accept a negative version of a point on the spacing scale, and output the equivalent spacing from the scale multiplied by -1. fixup! Allow govuk-spacing to output negative spacing
1 parent 69ab21b commit add5491

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

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: 46 additions & 0 deletions
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,6 +53,21 @@ describe('@function govuk-spacing', () => {
5253
top: 15px; }`)
5354
})
5455

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+
5571
it('throws an error when passed anything other than a number', async () => {
5672
const sass = `
5773
${sassBootstrap}
@@ -81,6 +97,36 @@ describe('@function govuk-spacing', () => {
8197
'Unknown spacing variable `999`. Make sure you are using a point from the spacing scale in `_settings/spacing.scss`.'
8298
)
8399
})
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+
})
84130
})
85131

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

0 commit comments

Comments
 (0)