forked from rauchg/slackin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.js
36 lines (31 loc) · 1.09 KB
/
badge.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
import svg from 'vd'
const title = 'slack'
const color = '#E01563'
const pad = 8 // left / right padding
const sep = 4 // middle separation
export default function badge ({ total, active }){
let value = active ? `${active}/${total}` : ('' + total || '–')
let lw = pad + width(title) + sep // left side width
let rw = sep + width(value) + pad // right side width
let tw = lw + rw // total width
return svg(`svg xmlns="http://www.w3.org/2000/svg" width=${tw} height=20`,
svg(`rect rx=3 width=${tw} height=20 fill=#555`),
svg(`rect rx=3 x=${lw} width=${rw} height=20 fill=${color}`),
svg(`path d="M${lw} 0h${sep}v20h-${sep}z" fill=${color}`),
svg('g text-anchor=middle font-family=Verdana font-size=11',
text({ str: title, x: Math.round(lw / 2), y: 14 }),
text({ str: value, x: lw + Math.round(rw / 2), y: 14 })
)
)
}
// generate text with 1px shadow
function text ({str, x, y}){
return [
svg(`text fill=#010101 fill-opacity=.3 x=${x} y=${y + 1}`, str),
svg(`text fill=#fff x=${x} y=${y}`, str)
]
}
// π=3
function width (str){
return 7 * str.length
}