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

Commit

Permalink
Add u-svg-inline-bg mixin for inline background SVG icons
Browse files Browse the repository at this point in the history
  • Loading branch information
anselmbradford committed Jul 9, 2019
1 parent b574625 commit aec76e0
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 32 deletions.
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( @type )`,
where `@type` 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
5 changes: 1 addition & 4 deletions packages/cf-forms/src/atoms/select.less
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@
background-color: @select-icon-bg;
content: '';
pointer-events: none;
background-image: url( '@{cf-icon-path}/down.svg' );
background-size: auto @cf-icon-height;
background-repeat: no-repeat;
background-position: center center;
.u-svg-inline-bg( 'down' );
}
}

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
5 changes: 1 addition & 4 deletions packages/cf-forms/src/molecules/form-fields.less
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@
}

&:checked + .a-label:before {
background-image: url( '@{cf-icon-path}/approved.svg' );
background-size: auto @cf-icon-height;
background-repeat: no-repeat;
background-position: center 0;
.u-svg-inline-bg( 'approved' );
}
}
}
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
20 changes: 20 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,20 @@
const path = require( 'path' );

module.exports = {
install: function( less, pluginManager, functions ) {
functions.add( 'cf-icons-svg-inline', svgName => {
// Retrieve this plugin scripts 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;
} );
}
}
30 changes: 23 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,47 @@
// 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 1200 (h) x 1000 (w).
// 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( @type ) {
@svg: cf-icons-svg-inline( @type );
background-image: url( 'data:image/svg+xml;charset=UTF-8,@{svg}' );
background-size: auto @cf-icon-height;
background-repeat: no-repeat;
background-position: center center;
height: @cf-icon-height;
}

//
// 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( @type )`,
where `@type` is the SVG icon canonical name.

## Rotating update icon

Expand Down
7 changes: 5 additions & 2 deletions packages/cf-tables/src/cf-tables.less
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//

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

// Mixins
.striped-table() {
Expand Down Expand Up @@ -86,9 +87,11 @@
.sortable:after {
display: inline-block;
position: relative;
top: -1px;
content: url( data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAJCAYAAAACTR1pAAAAXUlEQVR4AWPABZw9/C4A8Qlscvg0LQXi/1A8k1hNjXBNCFxNSFMcXDEmjsOlyQakgAB2RNckD8S/idD4D4jVYZq4gfgJXJIwfgPEIiCNt0ECJOKXII13gPgTifgaAHk0kQqUakHYAAAAAElFTkSuQmCC );
top: 4px;
content: '';
visibility: hidden;
.u-svg-inline-bg( 'down' );
width: 1em;
}

.sortable:hover:after,
Expand Down

0 comments on commit aec76e0

Please sign in to comment.