-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom icon support for adding new and overriding existing icon…
…s. (#366) * Added custom icon support for adding new and overriding existing icons. * Updated tests. * Custom icons will set default icons on the first property retrieval. * Reset library properties on build. * Added doc for custom icon and small improvement
- Loading branch information
Showing
9 changed files
with
498 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
let iconProps = {} | ||
let hasRunOnce = false | ||
|
||
/** | ||
* Add icon properties from a require.context() response to `iconProps`. | ||
* | ||
* Can be used as: | ||
* addIconsToLibrary(require.context('./custom_assets/', true, /\.svg$/)) | ||
* | ||
* @param {Function} webpackContext The function returned from a require.context() call. | ||
*/ | ||
function addIconsToLibrary (webpackContext) { | ||
webpackContext.keys().forEach(key => { | ||
const icon = webpackContext(key) | ||
if (icon.default) { | ||
const viewBoxSplit = icon.default.viewBox.split(' ') | ||
if (!iconProps[icon.default.id]) { | ||
iconProps[icon.default.id] = { | ||
width: parseFloat(viewBoxSplit[2]), | ||
height: parseFloat(viewBoxSplit[3]) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
function getIconProps () { | ||
// Add default icons on first call. | ||
// For correct overrides; this must run after custom icons have been added. | ||
if (!hasRunOnce) { | ||
hasRunOnce = true | ||
// In Jest there is no webpack require.context support, also we don't transform files. | ||
// Let's skip this part in tests. | ||
if (process.env.NODE_ENV !== 'test') { | ||
addIconsToLibrary(require.context('./assets/img/', true, /\.svg$/)) | ||
} | ||
} | ||
return iconProps | ||
} | ||
|
||
/** | ||
* Reset properties. | ||
* Use before calling addIconsToLibrary() to allow property reset on dev mode hot reload. | ||
*/ | ||
function resetLibrary () { | ||
iconProps = {} | ||
hasRunOnce = false | ||
} | ||
|
||
/** | ||
* Add custom icon properties from a require.context() response. | ||
* Custom icons should be added before the ripple-icon component is used. | ||
* | ||
* Can be used as: | ||
* addCustomIcons(require.context('./custom_assets/', true, /\.svg$/)) | ||
* | ||
* @param {Function} webpackContext The function returned from a require.context() call. | ||
*/ | ||
function addCustomIcons (webpackContext) { | ||
resetLibrary() | ||
addIconsToLibrary(webpackContext) | ||
} | ||
|
||
export { addCustomIcons } | ||
export { getIconProps } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import RplIcon from './Icon.vue' | ||
import RplTextIcon from './TextIcon.vue' | ||
import { addCustomIcons } from './icon-library' | ||
|
||
export {RplIcon} | ||
export {RplTextIcon} | ||
export { addCustomIcons } | ||
export default RplIcon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,10 @@ | |
padding-top: 1px; | ||
padding-bottom: 1px; | ||
} | ||
|
||
.summary { | ||
color: #000 !important; | ||
} | ||
} | ||
|
||
.demo-content { | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.