Skip to content

Commit 4d29226

Browse files
OrnanovitchdavwheatSychO9
authored
fix(tags): tag text color contrast (#3653)
* add yiq calculator util * use the new contast util to differentiate light/dark tags * fix: invert logic * feat: add tag-dark and tag-light less config * fix: convert 3 chars hex to 6 chars hex * fix: rename import * fix: clarify util name * fix: rename function * fix: invert less variables when dark mode is enabled * fix: TagTiles contrast * refactor: simplify logic with a unique variable * refactor: simplify logic with a unique variable * feat: add text color variables not depending on the dark/light mode * refactor: use isDark rather than getContrast * refactor: change getContrast to isDark with for a more direct approach * fix: adjust snippet description * refactor: change getContrast to isDark with for a more direct approach * fix: adjust snippet description * fix: TagHero contrast * fix: DiscussionHero contrast * fix: newDiscussion contrast * fix(newDiscussion): restore less rule when tag is not colored * fix: TagTiles description * fix: TagTiles last posted * chore: change `var` to `let` * refactor: keep it for backwards compatibility * refactor: keep it for backwards compatibility * Apply suggestions from code review * fix: missed this when I was resolving * fix: remove dist files from pull request * Revert "Resolved merge conflict" This reverts commit c7f0d14, reversing changes made to 6753dfc. * fix: missed this when I was resolving * fix * Update isDark.ts * chore: flexible contrast color fixing * refactor(isDark): clarify the doc block * fix(isDark): increase the yiq threshold * typo * fix: preserve design coloring through light and dark modes Co-authored-by: David Wheatley <david@davwheat.dev> Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>
1 parent 6adae00 commit 4d29226

File tree

16 files changed

+75
-30
lines changed

16 files changed

+75
-30
lines changed

extensions/tags/js/src/common/helpers/tagLabel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import extract from 'flarum/common/utils/extract';
22
import Link from 'flarum/common/components/Link';
3+
import classList from 'flarum/common/utils/classList';
4+
import textContrastClass from 'flarum/common/helpers/textContrastClass';
35
import tagIcon from './tagIcon';
46

57
export default function tagLabel(tag, attrs = {}) {
@@ -13,7 +15,7 @@ export default function tagLabel(tag, attrs = {}) {
1315
const color = tag.color();
1416
if (color) {
1517
attrs.style['--tag-bg'] = color;
16-
attrs.className += ' colored';
18+
attrs.className = classList(attrs.className, 'colored', textContrastClass(color));
1719
}
1820

1921
if (link) {

extensions/tags/js/src/forum/addTagFilter.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import IndexPage from 'flarum/forum/components/IndexPage';
55
import DiscussionListState from 'flarum/forum/states/DiscussionListState';
66
import GlobalSearchState from 'flarum/forum/states/GlobalSearchState';
77
import classList from 'flarum/common/utils/classList';
8+
import textContrastClass from 'flarum/common/helpers/textContrastClass';
89

910
import TagHero from './components/TagHero';
1011
import Tag from '../common/models/Tag';
@@ -90,7 +91,7 @@ export default function () {
9091
const newDiscussion = items.get('newDiscussion') as Mithril.Vnode<ComponentAttrs, {}>;
9192

9293
if (color) {
93-
newDiscussion.attrs.className = classList([newDiscussion.attrs.className, 'Button--tagColored']);
94+
newDiscussion.attrs.className = classList([newDiscussion.attrs.className, 'Button--tagColored', textContrastClass(color)]);
9495
newDiscussion.attrs.style = { '--color': color };
9596
}
9697

extensions/tags/js/src/forum/addTagLabels.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { extend } from 'flarum/common/extend';
22
import DiscussionListItem from 'flarum/forum/components/DiscussionListItem';
33
import DiscussionHero from 'flarum/forum/components/DiscussionHero';
4+
import textContrastClass from 'flarum/common/helpers/textContrastClass';
5+
import classList from 'flarum/common/utils/classList';
46

57
import tagsLabel from '../common/helpers/tagsLabel';
68
import sortTags from '../common/utils/sortTags';
@@ -23,7 +25,7 @@ export default function () {
2325
const color = tags[0].color();
2426
if (color) {
2527
view.attrs.style = { '--hero-bg': color };
26-
view.attrs.className += ' DiscussionHero--colored';
28+
view.attrs.className = classList(view.attrs.className, 'DiscussionHero--colored', textContrastClass(color));
2729
}
2830
}
2931
});

extensions/tags/js/src/forum/components/TagHero.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import Component from 'flarum/common/Component';
2+
import textContrastClass from 'flarum/common/helpers/textContrastClass';
23
import tagIcon from '../../common/helpers/tagIcon';
4+
import classList from 'flarum/common/utils/classList';
35

46
export default class TagHero extends Component {
57
view() {
68
const tag = this.attrs.model;
79
const color = tag.color();
810

911
return (
10-
<header className={'Hero TagHero' + (color ? ' TagHero--colored' : '')} style={color ? { '--hero-bg': color } : ''}>
12+
<header
13+
className={classList('Hero', 'TagHero', { 'TagHero--colored': color }, textContrastClass(color))}
14+
style={color ? { '--hero-bg': color } : ''}
15+
>
1116
<div className="container">
1217
<div className="containerNarrow">
1318
<h2 className="Hero-title">

extensions/tags/js/src/forum/components/TagsPage.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Link from 'flarum/common/components/Link';
44
import LoadingIndicator from 'flarum/common/components/LoadingIndicator';
55
import listItems from 'flarum/common/helpers/listItems';
66
import humanTime from 'flarum/common/helpers/humanTime';
7+
import textContrastClass from 'flarum/common/helpers/textContrastClass';
8+
import classList from 'flarum/common/utils/classList';
79

810
import tagIcon from '../../common/helpers/tagIcon';
911
import tagLabel from '../../common/helpers/tagLabel';
@@ -58,7 +60,7 @@ export default class TagsPage extends Page {
5860
const children = sortTags(tag.children() || []);
5961

6062
return (
61-
<li className={'TagTile ' + (tag.color() ? 'colored' : '')} style={{ '--tag-bg': tag.color() }}>
63+
<li className={classList('TagTile', { colored: tag.color() }, textContrastClass(tag.color()))} style={{ '--tag-bg': tag.color() }}>
6264
<Link className="TagTile-info" href={app.route.tag(tag)}>
6365
{tag.icon() && tagIcon(tag, {}, { useColor: false })}
6466
<h3 className="TagTile-name">{tag.name()}</h3>

extensions/tags/less/common/TagLabel.less

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717

1818
&.colored {
19-
--tag-color: @body-bg;
19+
--tag-color: var(--contrast-color, var(--body-bg));
2020

2121
.TagLabel-text {
2222
color: var(--tag-color) !important;
@@ -31,13 +31,13 @@
3131
&.colored {
3232
--tag-color: var(--tag-bg);
3333
margin-right: 5px;
34-
background: @body-bg !important;
34+
background-color: var(--contrast-color, var(--body-bg));
3535
}
3636
}
3737
}
3838
.DiscussionHero--colored {
3939
&, a {
40-
color: @body-bg;
40+
color: var(--contrast-color, var(--body-bg));
4141
}
4242
}
4343
.TagsLabel {

extensions/tags/less/forum.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
--button-primary-bg: var(--color);
1010
--button-primary-bg-hover: var(--color);
1111
--button-primary-bg-active: var(--color);
12+
--button-color: var(--contrast-color);
1213
}
1314
.DiscussionHero {
1415
.item-title {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.TagHero {
22
&--colored {
3-
--hero-color: #fff;
3+
--hero-color: var(--body-bg);
44
}
55
}

extensions/tags/less/forum/TagTiles.less

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
}
6565
&.colored {
6666
&, a {
67-
color: @body-bg;
67+
color: var(--contrast-color, var(--body-bg));
6868
}
6969
}
7070
}
@@ -100,10 +100,7 @@
100100
.TagTile-description {
101101
font-size: 12px;
102102
margin: 0 0 10px;
103-
104-
.TagTile.colored & {
105-
color: fade(@body-bg, 70%);
106-
}
103+
opacity: 70%;
107104
}
108105
.TagTile-children {
109106
font-weight: bold;
@@ -124,10 +121,7 @@
124121
font-size: 12px;
125122
border-top: 1px solid rgba(0, 0, 0, 0.15);
126123
margin: 0 20px;
127-
128-
.TagTile.colored & {
129-
color: fade(@body-bg, 70%);
130-
}
124+
opacity: 70%;
131125

132126
&:hover .TagTile-lastPostedDiscussion-title {
133127
text-decoration: underline;

framework/core/js/src/common/compat.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import highlight from './helpers/highlight';
8181
import username from './helpers/username';
8282
import userOnline from './helpers/userOnline';
8383
import listItems from './helpers/listItems';
84+
import textContrastClass from './helpers/textContrastClass';
8485
import Fragment from './Fragment';
8586
import DefaultResolver from './resolvers/DefaultResolver';
8687
import PaginatedListState from './states/PaginatedListState';
@@ -175,6 +176,7 @@ export default {
175176
'helpers/username': username,
176177
'helpers/userOnline': userOnline,
177178
'helpers/listItems': listItems,
179+
'helpers/textContrastClass': textContrastClass,
178180
'resolvers/DefaultResolver': DefaultResolver,
179181
'states/PaginatedListState': PaginatedListState,
180182
'states/AlertManagerState': AlertManagerState,

0 commit comments

Comments
 (0)