Skip to content

Commit 0cf6487

Browse files
committed
invert text color in colored tags
1 parent 699006a commit 0cf6487

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

browser/components/NoteItem.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import PropTypes from 'prop-types'
55
import React from 'react'
66
import { isArray } from 'lodash'
7+
import invertColor from 'invert-color'
78
import CSSModules from 'browser/lib/CSSModules'
89
import { getTodoStatus } from 'browser/lib/getTodoStatus'
910
import styles from './NoteItem.styl'
@@ -16,11 +17,18 @@ import i18n from 'browser/lib/i18n'
1617
* @param {string} color
1718
* @return {React.Component}
1819
*/
19-
const TagElement = ({ tagName, color }) => (
20-
<span styleName='item-bottom-tagList-item' key={tagName} style={{backgroundColor: color}}>
21-
#{tagName}
22-
</span>
23-
)
20+
const TagElement = ({ tagName, color }) => {
21+
const style = {}
22+
if (color) {
23+
style.backgroundColor = color
24+
style.color = invertColor(color, { black: '#222', white: '#f1f1f1', threshold: 0.3 })
25+
}
26+
return (
27+
<span styleName='item-bottom-tagList-item' key={tagName} style={style}>
28+
#{tagName}
29+
</span>
30+
)
31+
}
2432

2533
/**
2634
* @description Tag element list component.

browser/main/Detail/TagSelect.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import PropTypes from 'prop-types'
22
import React from 'react'
3+
import invertColor from 'invert-color'
34
import CSSModules from 'browser/lib/CSSModules'
45
import styles from './TagSelect.styl'
56
import _ from 'lodash'
@@ -183,12 +184,19 @@ class TagSelect extends React.Component {
183184

184185
const tagList = _.isArray(value)
185186
? (showTagsAlphabetically ? _.sortBy(value) : value).map((tag) => {
187+
const wrapperStyle = {}
188+
const textStyle = {}
189+
const color = coloredTags[tag]
190+
if (color) {
191+
wrapperStyle.backgroundColor = color
192+
textStyle.color = invertColor(color, { black: '#222', white: '#f1f1f1' })
193+
}
186194
return (
187195
<span styleName='tag'
188196
key={tag}
189-
style={{backgroundColor: coloredTags[tag]}}
197+
style={wrapperStyle}
190198
>
191-
<span styleName='tag-label' onClick={(e) => this.handleTagLabelClick(tag)}>#{tag}</span>
199+
<span styleName='tag-label' style={textStyle} onClick={(e) => this.handleTagLabelClick(tag)}>#{tag}</span>
192200
<button styleName='tag-removeButton'
193201
onClick={(e) => this.handleTagRemoveButtonClick(tag)}
194202
>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"i18n-2": "^0.7.2",
7070
"iconv-lite": "^0.4.19",
7171
"immutable": "^3.8.1",
72+
"invert-color": "^2.0.0",
7273
"js-sequence-diagrams": "^1000000.0.6",
7374
"js-yaml": "^3.12.0",
7475
"katex": "^0.9.0",

0 commit comments

Comments
 (0)