[tooltip] Update tooltip pug to mixin
This commit is contained in:
@@ -1,2 +1,44 @@
|
|||||||
a(href="#") Link with a tool tip
|
mixin tooltip(a)
|
||||||
span(role="tooltip" inert tip-position="right") Tool tip content
|
-
|
||||||
|
const required = ['text']
|
||||||
|
const optional = ['tip-position', 'tip-align', 'inert', 'role' ].concat(required)
|
||||||
|
|
||||||
|
// function to strip unallowed options.
|
||||||
|
const allowed = (obj, whitelist) => {
|
||||||
|
const set = new Set(whitelist);
|
||||||
|
const result = {};
|
||||||
|
for (const [k, v] of Object.entries(obj)) {
|
||||||
|
if (set.has(k)) result[k] = v;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// default values
|
||||||
|
const defaults = {
|
||||||
|
"tip-position": "right",
|
||||||
|
inert: "inert",
|
||||||
|
role: "tooltip",
|
||||||
|
}
|
||||||
|
|
||||||
|
// unhyphenated to hypenated
|
||||||
|
const map = {
|
||||||
|
position: 'tip-position',
|
||||||
|
align: 'tip-align'
|
||||||
|
}
|
||||||
|
for (const [from, to] of Object.entries(map)) {
|
||||||
|
if (a[from] !== undefined) {
|
||||||
|
a[to] = a[from]
|
||||||
|
delete a[from]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// recheck for required and output if everything required is there.
|
||||||
|
if (a && required.some(key => !(key in a)))
|
||||||
|
p Missing keys: #{required.filter(key => !(key in a))}
|
||||||
|
else
|
||||||
|
-
|
||||||
|
text = a.text
|
||||||
|
a.text = undefined
|
||||||
|
a = allowed({...defaults, ...a}, optional)
|
||||||
|
|
||||||
|
tool-tip&attributes(a)!= text
|
||||||
Reference in New Issue
Block a user