-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
50 lines (44 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module.exports = function icons (paths, prefix) {
function initialize (host, names) {
if (typeof host === 'undefined') throw new Error('A host-dom element is required. Initialize is only supported in browsers.')
var doc = host.ownerDocument
var div = doc.createElement('div')
div.innerHTML = getSymbols(names)
host.appendChild(div.firstChild)
}
function getSVG (name, attributes) {
if (typeof paths[name] !== 'string') return
var path = paths[name]
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"'+ (attributes || '') +'>' + getPath(path) + '</svg>'
}
function getSymbols (names) {
if (!names) names = Object.keys(paths)
var symbols = names.map(function (name) { return toSymbol(name, paths[name], prefix) }).filter(Boolean)
return '<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">' + symbols.join('') + '</svg>'
}
function getIcon (name, attributes) {
if (!paths[name]) return
var id = prefix? prefix + '-' + name : name
return '<svg ' + (attributes || '') + '><use xlink:href="#' + id + '" /></svg>'
}
return {
initialize: initialize,
getSVG: getSVG,
getSymbols: getSymbols,
getIcon: getIcon
}
}
function getPath (path) {
path = path.trim()
if (/^<svg/.test(path)) return path
if (/^<path/.test(path)) return path
return '<path d="' + path + '" />'
}
function toSymbol (name, path, prefix) {
if (!path) return
var id = prefix? prefix + '-' + name : name
return '<symbol id="' + id + '" viewBox="0 0 24 24">' +
'<title>' + name + '</title>' +
getPath(path) +
'</symbol>'
}