Skip to content

Commit bc4a67c

Browse files
yantantethernhsbsa-pattu
authored andcommitted
Added Nunjucks macros for colorBlock, colorCard
colorBlock shows a block of color for display in playbook colorCard shows a shows a block of color alongside various color properties such as Hex, RGB, CMYK Uses colorConvert dependency Fixes minor SonarQube issues
1 parent ef67ddc commit bc4a67c

File tree

14 files changed

+5738
-3
lines changed

14 files changed

+5738
-3
lines changed

.eleventy.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = function (eleventyConfig) {
2929
});
3030
eleventyConfig.addPlugin(require('eleventy-plugin-external-links'), {
3131
name: 'external-links',
32-
regex: /^((http[s]?:)|(\/\/))/i, // Regex that test if href is external
32+
regex: /^((https?:)|(\/\/))/i, // Regex that test if href is external
3333
target: '_blank',
3434
rel: 'noopener',
3535
extensions: ['.html'],
@@ -38,6 +38,10 @@ module.exports = function (eleventyConfig) {
3838
eleventyConfig.addPlugin(require('eleventy-auto-cache-buster'));
3939

4040
// filters
41+
eleventyConfig.addFilter(
42+
'convertColor',
43+
require('./lib/_filters/convertColor'),
44+
);
4145
eleventyConfig.addFilter('date', require('./lib/_filters/date'));
4246
eleventyConfig.addFilter('fixed', require('./lib/_filters/fixed'));
4347
eleventyConfig.addFilter('markdown', require('./lib/_filters/markdown'));

lib/_components/_all.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
@import "appCard/card";
33
@import "appFooter/footer";
44
@import "appPagination/pagination";
5-
@import "related/related";
5+
@import "related/related";
6+
@import "colorBlock/colorBlock";
7+
@import "colorCard/colorCard";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.colorblock {
2+
display: inline-block;
3+
border: 1px solid black;
4+
}
5+
6+
// mixin used here for creating size variations
7+
@mixin colorblock-size($size) {
8+
width: $size;
9+
height: $size;
10+
max-width: $size;
11+
max-height: $size;
12+
}
13+
14+
$sizes: (
15+
'xs': 10px,
16+
's': 20px,
17+
'm': 50px,
18+
'l': 100px,
19+
'xl': 200px
20+
);
21+
22+
@each $key, $size in $sizes {
23+
.colorblock_#{$key} {
24+
@extend .colorblock;
25+
@include colorblock-size($size);
26+
}
27+
}

lib/_components/colorBlock/macro.njk

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro colorBlock(params) %}
2+
{%- include "./template.njk" -%}
3+
{% endmacro %}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span style="background-color: {{params.color}}" class="colorblock colorblock_{{params.blockSize if params.blockSize else 'm'}}"></span>
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.colorCard > dl {
2+
padding: 0;
3+
margin: 0;
4+
}
5+
.colorCard > dl > dd {
6+
padding: 0;
7+
margin-left: 100px;
8+
}
9+
@media (max-width: 640px) {
10+
.colorCard > dl > dd {
11+
padding: 0;
12+
margin: 0;
13+
}
14+
}
15+
@media (min-width: 641px) {
16+
.colorCard > dl > dt {
17+
padding: 0;
18+
margin: 0 10px 0 0;
19+
width: 100px;
20+
}
21+
}
22+
@media (max-width: 640px) {
23+
.colorCard > dl > dt {
24+
padding: 0;
25+
margin: 0 10px 0 0;
26+
}
27+
}

lib/_components/colorCard/macro.njk

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro colorCard(params) %}
2+
{%- include "./template.njk" -%}
3+
{% endmacro %}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{% from "colorBlock/macro.njk" import colorBlock %}
2+
{% set headingLevel %}
3+
{{ params.headingLevel if params.headingLevel else 'h2' }}
4+
{% endset %}
5+
<div class="card colorCard">
6+
{% if params.heading %}
7+
<{{headingLevel}} class="nhsuk-card__heading nhsuk-heading-m">
8+
{{ params.heading }}
9+
</{{headingLevel}}>
10+
{% endif %}
11+
<div>{{ colorBlock(params) }}</div>
12+
<dl>
13+
<dt>Hex</dt>
14+
<dd>{{params.color}}</dd>
15+
<dt>RGB</dt>
16+
<dd>{{params.color | convertColor('RGB') }}</dd>
17+
<dt>CMYK</dt>
18+
<dd>{{params.color | convertColor('CMYK') }}</dd>
19+
{% if params.pantone %}
20+
<dt>Pantone</dt>
21+
<dd>{{params.pantone}}</dd>
22+
{% endif %}
23+
{% if params.ral %}
24+
<dt>RAL</dt>
25+
<dd>{{params.ral}}</dd>
26+
{% endif %}
27+
</dl>
28+
</div>

lib/_filters/convertColor.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let convert = require('color-convert');
2+
3+
function hexToRgb(hex) {
4+
return convert.hex.rgb(hex);
5+
}
6+
function hexToCmyk(hex) {
7+
return convert.hex.cmyk(hex);
8+
}
9+
function stringify(values) {
10+
return values ? values.join(',') : values;
11+
}
12+
let convertColor = (value, format) => {
13+
if (value) {
14+
if ('CMYK' === format) {
15+
return stringify(hexToCmyk(value));
16+
}
17+
if ('RGB' === format) {
18+
return stringify(hexToRgb(value));
19+
}
20+
}
21+
return value;
22+
};
23+
24+
module.exports = convertColor;
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const convertColor = require('../../_filters/convertColor');
2+
3+
describe('blank', () => {
4+
test('should export module as a function', () => {
5+
expect(typeof convertColor).toBe('function');
6+
});
7+
8+
test.each([
9+
{ value: '#000000', format: 'RGB', expected: '0,0,0' },
10+
{ value: '#FFFFFF', format: 'RGB', expected: '255,255,255' },
11+
{ value: '#FF0000', format: 'RGB', expected: '255,0,0' },
12+
{ value: '#00FF00', format: 'RGB', expected: '0,255,0' },
13+
{ value: '#0000FF', format: 'RGB', expected: '0,0,255' },
14+
])('should return RGB when input is: %s', ({ value, format, expected }) => {
15+
expect(convertColor(value, format)).toBe(expected);
16+
});
17+
18+
test.each([
19+
{ value: '#000000', format: 'CMYK', expected: '0,0,0,100' },
20+
{ value: '#FFFFFF', format: 'CMYK', expected: '0,0,0,0' },
21+
{ value: '#FF0000', format: 'CMYK', expected: '0,100,100,0' },
22+
{ value: '#00FF00', format: 'CMYK', expected: '100,0,100,0' },
23+
{ value: '#0000FF', format: 'CMYK', expected: '100,100,0,0' },
24+
{ value: '#00FFFF', format: 'CMYK', expected: '100,0,0,0' },
25+
{ value: '#FF00FF', format: 'CMYK', expected: '0,100,0,0' },
26+
{ value: '#FFFF00', format: 'CMYK', expected: '0,0,100,0' },
27+
])('should return CMYK when input is: %s', ({ value, format, expected }) => {
28+
expect(convertColor(value, format)).toBe(expected);
29+
});
30+
});

0 commit comments

Comments
 (0)