Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Add u-svg-inline-bg mixin for inline SVG background #938

Merged
merged 2 commits into from
Jul 23, 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
7 changes: 7 additions & 0 deletions docs/_includes/usage/cf-icons/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ match the height. The whitespace to the left or right may not be quite
accurate, but we determined this is an acceptable difference for a legacy
browser like IE9.

## Inline SVG background

In some cases we embed an SVG as a background image.
To accomplish this, a custom less plugin is used to inject the SVG icon source
file inline into the CSS `background-image` property.
This is exposed via a mixin, `.u-svg-inline-bg( @name )`,
where `@name` is the SVG icon canonical name.

## Rotating update icon

Expand Down
1 change: 1 addition & 0 deletions docs/_includes/usage/cf-typography/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and has more basic typography patterns.

- [Variables](#variables)
- [Color variables](#color-variables)
- [Font variables](#font-variables)
- [Font source variables](#font-source-variables)
- [Headings](#headings)
- [Heading with icon](#heading-with-icon)
Expand Down
2 changes: 1 addition & 1 deletion docs/dist/css/main.css

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/dist/icons/approved.svg

This file was deleted.

1 change: 0 additions & 1 deletion docs/dist/icons/down.svg

This file was deleted.

4 changes: 0 additions & 4 deletions docs/src/css/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
@import (less) "../../../packages/cf-expandables/src/cf-expandables.less";
@import (less) "../../../packages/cf-tables/src/cf-tables.less";

// Icon font path.
// When SVGs are used as e.g. background images, we need a path to them.
@cf-icon-path: '../icons';

// Webfont variables
// This is the path for self-hosted fonts.
@cf-fonts-path: '/static/fonts';
Expand Down
2 changes: 1 addition & 1 deletion packages/cf-forms/src/atoms/select.less
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
background-color: @select-icon-bg;
content: '';
pointer-events: none;
background-image: url( '@{cf-icon-path}/down.svg' );
.u-svg-inline-bg( 'down' );
background-size: auto @cf-icon-height;
background-repeat: no-repeat;
background-position: center center;
Expand Down
5 changes: 0 additions & 5 deletions packages/cf-forms/src/cf-forms.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
// Theme variables
//

// Default icon font path.
// When SVGs are used for background images, we need a path to them.
// May be overridden when this package is imported into implementing less files.
@cf-icon-path: '../icons';

// Color variables

// .a-text-input borders
Expand Down
2 changes: 1 addition & 1 deletion packages/cf-forms/src/molecules/form-fields.less
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
}

&:checked + .a-label:before {
background-image: url( '@{cf-icon-path}/approved.svg' );
.u-svg-inline-bg( 'approved' );
background-size: auto @cf-icon-height;
background-repeat: no-repeat;
background-position: center 0;
Expand Down
3 changes: 0 additions & 3 deletions packages/cf-icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"description": "Capital Framework icons",
"less": "src/cf-icons.less",
"style": "cf-icons.css",
"dependencies": {
"cf-core": "^10.2.1"
},
"keywords": [
"icons"
]
Expand Down
30 changes: 30 additions & 0 deletions packages/cf-icons/src/cf-icons-svg-inline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require( 'path' );
Scotchester marked this conversation as resolved.
Show resolved Hide resolved

/**
* This file is a less plugin that gets included in a less file via the
* `@plugin "cf-icons-svg-inline";` syntax.
* The `install` function and the `function.add()` method are the hooks
* necessary for the less preprocessor to pick this up and run it at build-time.
* The plugin name specified in the less file, this file's filename,
* and the name passed to `function.add()` must all be the same.
* See http://lesscss.org/features/#plugin-atrules-feature-writing-your-first-plugin
*/

module.exports = {
install: function( less, pluginManager, functions ) {
functions.add( 'cf-icons-svg-inline', svgName => {
// Retrieve this plugin script's path so we can fake __dirname.
const thisScriptPath = less.importManager.context.pluginManager.installedPlugins[0].filename;

// __dirname is not accessible in this script, so this fakes it.
const __dirname = path.dirname( thisScriptPath );

const svg = less.fs.readFileSync(
path.join( __dirname, `./icons/${ svgName.value }.svg` ),
'utf8'
);

return svg;
} );
}
}
26 changes: 19 additions & 7 deletions packages/cf-icons/src/cf-icons.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,43 @@
// Size variables
//

// Icon height matches the 19px rendered canvas of text set in Avenir Next
// sized at 16px ( 19/16 = 1.1875 )
// Icon SVGs viewbox is 1000 (w) x 1200 (h).
// The height matches the 19px rendered canvas of text set in Avenir Next
// sized at 16px (19/16 = 1.1875).
@cf-icon-height: 1.1875em;

//
// Embedded inline SVG data URLs.
//

// Mixin to inject an SVG from the ./icons/ directory
// into a background-image property.
@plugin "cf-icons-svg-inline";
.u-svg-inline-bg( @name ) {
@svg: cf-icons-svg-inline( @name );
background-image: url( 'data:image/svg+xml;charset=UTF-8,@{svg}' );
}

//
// The basics
// The basics.
//

.cf-icon-svg {
height: @cf-icon-height;
vertical-align: text-top;
fill: currentColor;

// IE 10 & 11 require a max-width otherwise the SVG takes up 100%
// IE 10 & 11 require a max-width otherwise the SVG takes up 100%.
max-width: 1em;

.lt-ie10 & {
// IE 9 require a width otherwise the SVG takes up 100%
// IE 9 require a width otherwise the SVG takes up 100%.
width: 1em;
}

.lt-ie9 & {
// IE 8 doesn't support currentColor, hide icons and let the paired
// text stand on its own
// IE 8 doesn't support currentColor;
// hide icons and let the paired text stand on its own.
display: none;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/cf-icons/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ match the height. The whitespace to the left or right may not be quite
accurate, but we determined this is an acceptable difference for a legacy
browser like IE9.

## Inline SVG background

In some cases we embed an SVG as a background image.
To accomplish this, a custom less plugin is used to inject the SVG icon source
file inline into the CSS `background-image` property.
This is exposed via a mixin, `.u-svg-inline-bg( @name )`,
where `@name` is the SVG icon canonical name.

## Rotating update icon

Expand Down
26 changes: 18 additions & 8 deletions packages/cf-tables/src/cf-tables.less
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
// Import external dependencies
//

@import '../../cf-core/src/cf-core.less';
@import (reference) '../../cf-core/src/cf-core.less';
@import (reference) '../../cf-icons/src/cf-icons.less';

// Mixins
.striped-table() {
Expand Down Expand Up @@ -81,14 +82,20 @@
outline: none;
text-align: left;
text-transform: inherit;
}

.sortable:after {
display: inline-block;
position: relative;
top: -1px;
content: url( data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAJCAYAAAACTR1pAAAAXUlEQVR4AWPABZw9/C4A8Qlscvg0LQXi/1A8k1hNjXBNCFxNSFMcXDEmjsOlyQakgAB2RNckD8S/idD4D4jVYZq4gfgJXJIwfgPEIiCNt0ECJOKXII13gPgTifgaAHk0kQqUakHYAAAAAElFTkSuQmCC );
visibility: hidden;
&:after {
display: inline-block;
position: relative;
vertical-align: bottom;
content: '';
visibility: hidden;
.u-svg-inline-bg( 'down' );
background-size: auto @cf-icon-height;
background-repeat: no-repeat;
background-position: center center;
height: @cf-icon-height;
width: 1em;
}
}

.sortable:hover:after,
Expand All @@ -99,10 +106,13 @@

.sortable.sorted-down:after,
.sortable.sorted-up:hover:after {
top: 1px;
transform: rotate( 180deg );
}

.sortable.sorted-up:after,
.sortable.sorted-down:hover:after {
top: -1px;
transform: rotate( 0deg );
}
}
Expand Down