[1.x] Custom Colorising with CSS Custom Properties (#3001)

* Start of conversion to CSS variables
* Use variable for Badge colors
* Use variable for avatar bg
* Use variable for user card bg
* Use css variables for hero
* Use css variables for buttons
* Use css variables for sidenav links
* Cleaner style attr

Co-authored-by: David Wheatley <hi@davwheat.dev>
This commit is contained in:
Sami Mazouz 2021-08-16 10:17:48 +01:00 committed by GitHub
parent ac4cd4f84d
commit ace08b4869
16 changed files with 50 additions and 37 deletions

View File

@ -32,7 +32,7 @@ export default class EditGroupModal extends Modal {
this.color() || this.icon() this.color() || this.icon()
? Badge.component({ ? Badge.component({
icon: this.icon(), icon: this.icon(),
style: { backgroundColor: this.color() }, color: this.color(),
}) })
: '', : '',
' ', ' ',

View File

@ -18,15 +18,18 @@ import classList from '../utils/classList';
*/ */
export default class Badge extends Component { export default class Badge extends Component {
view() { view() {
const { type, icon: iconName, label, ...attrs } = this.attrs; const { type, icon: iconName, label, color, style = {}, ...attrs } = this.attrs;
const className = classList('Badge', [type && `Badge--${type}`], attrs.className); const className = classList('Badge', [type && `Badge--${type}`], attrs.className);
const iconChild = iconName ? icon(iconName, { className: 'Badge-icon' }) : m.trust('&nbsp;'); const iconChild = iconName ? icon(iconName, { className: 'Badge-icon' }) : m.trust('&nbsp;');
const newStyle = { ...style, '--badge-bg': color };
const badgeAttrs = { const badgeAttrs = {
...attrs, ...attrs,
className, className,
style: newStyle,
}; };
const badgeNode = <div {...badgeAttrs}>{iconChild}</div>; const badgeNode = <div {...badgeAttrs}>{iconChild}</div>;

View File

@ -6,7 +6,7 @@ export default class GroupBadge extends Badge {
if (attrs.group) { if (attrs.group) {
attrs.icon = attrs.group.icon(); attrs.icon = attrs.group.icon();
attrs.style = { backgroundColor: attrs.group.color() }; attrs.color = attrs.group.color();
attrs.label = typeof attrs.label === 'undefined' ? attrs.group.nameSingular() : attrs.label; attrs.label = typeof attrs.label === 'undefined' ? attrs.group.nameSingular() : attrs.label;
attrs.type = 'group--' + attrs.group.id(); attrs.type = 'group--' + attrs.group.id();

View File

@ -31,7 +31,7 @@ export default function avatar(user: User, attrs: Object = {}): Mithril.Vnode {
} }
content = username.charAt(0).toUpperCase(); content = username.charAt(0).toUpperCase();
attrs.style = { background: user.color() }; attrs.style = { '--avatar-bg': user.color() };
} }
return <span {...attrs}>{content}</span>; return <span {...attrs}>{content}</span>;

View File

@ -30,7 +30,7 @@ export default class UserCard extends Component {
const badges = user.badges().toArray(); const badges = user.badges().toArray();
return ( return (
<div className={'UserCard ' + (this.attrs.className || '')} style={color ? { backgroundColor: color } : ''}> <div className={'UserCard ' + (this.attrs.className || '')} style={color && { '--usercard-bg': color }}>
<div className="darkenBackground"> <div className="darkenBackground">
<div className="container"> <div className="container">
{controls.length {controls.length

View File

@ -13,7 +13,7 @@
margin-bottom: 20px; margin-bottom: 20px;
.Button { .Button {
.Button--color(@control-color, @body-bg) .Button--color(@control-color, @body-bg, 'button-alternate')
} }
} }

View File

@ -4,7 +4,7 @@
color: #fff; color: #fff;
text-align: center; text-align: center;
vertical-align: top; vertical-align: top;
background-color: @control-bg; background-color: var(--avatar-bg);
font-weight: normal; font-weight: normal;
.Avatar--size(48px); .Avatar--size(48px);

View File

@ -1,7 +1,7 @@
.Badge { .Badge {
.Badge--size(22px); .Badge--size(22px);
background: @muted-color; background: var(--badge-bg);
color: #fff; color: var(--badge-color);
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
text-align: center; text-align: center;

View File

@ -53,7 +53,7 @@
padding: 8px 13px; padding: 8px 13px;
border-radius: @border-radius; border-radius: @border-radius;
.user-select(none); .user-select(none);
.Button--color(@control-color, @control-bg); .Button--color(@control-color, @control-bg, 'button');
border: 0; border: 0;
&:hover { &:hover {
@ -98,26 +98,28 @@
} }
} }
.Button--color(@color; @background) { .Button--color(@color; @background; @name: 'button') {
color: @color; color: var(~"--@{name}-color", ~"@{color}");
background: @background; background: var(~"--@{name}-bg", ~"@{background}");
@bg-hover: darken(fadein(@background, 5%), 5%);
@bg-active: darken(fadein(@background, 10%), 10%);
&:hover, &:hover,
&:focus, &:focus,
&.focus { &.focus {
background-color: darken(fadein(@background, 5%), 5%); background-color: var(~"--@{name}-bg-hover", ~"@{bg-hover}");
} }
&:active, &:active,
&.active, &.active,
.open > .Dropdown-toggle& { .open > .Dropdown-toggle& {
background-color: darken(fadein(@background, 10%), 10%); background-color: var(~"--@{name}-bg-active", ~"@{bg-active}");
} }
&.disabled, &.disabled,
&[disabled], &[disabled],
fieldset[disabled] & { fieldset[disabled] & {
background: @background; background: var(~"--@{name}-bg-disabled", ~"@{background}");
} }
} }
@ -166,7 +168,7 @@
} }
} }
.Button--primary { .Button--primary {
.Button--color(@body-bg, @primary-color); .Button--color(@body-bg, @primary-color, 'button-primary');
font-weight: bold; font-weight: bold;
padding-left: 20px; padding-left: 20px;
padding-right: 20px; padding-right: 20px;
@ -176,7 +178,7 @@
} }
} }
.Button--danger { .Button--danger {
.Button--color(@control-danger-color, @control-danger-bg); .Button--color(@control-danger-color, @control-danger-bg, 'control-danger');
} }
.Button--more { .Button--more {
padding: 2px 4px; padding: 2px 4px;

View File

@ -6,6 +6,7 @@
@import "normalize"; @import "normalize";
@import "print"; @import "print";
@import "root";
@import "scaffolding"; @import "scaffolding";
@import "sideNav"; @import "sideNav";

View File

@ -0,0 +1,18 @@
:root {
// Component colors
--avatar-bg: @control-bg;
--badge-bg: @muted-color;
--badge-color: #fff;
--usercard-bg: @control-bg;
--hero-bg: @hero-bg;
--hero-color: @hero-color;
// Store the current responsive screen mode in a CSS variable, to make it
// available to the JS code.
--flarum-screen: none;
@media @phone { --flarum-screen: phone; }
@media @tablet { --flarum-screen: tablet; }
@media @desktop { --flarum-screen: desktop; }
@media @desktop-hd { --flarum-screen: desktop-hd; }
}

View File

@ -1,14 +1,3 @@
// Store the current responsive screen mode in a CSS variable, to make it
// available to the JS code.
:root {
--flarum-screen: none;
@media @phone { --flarum-screen: phone; }
@media @tablet { --flarum-screen: tablet; }
@media @desktop { --flarum-screen: desktop; }
@media @desktop-hd { --flarum-screen: desktop-hd; }
}
* { * {
&, &,
&:before, &:before,

View File

@ -19,11 +19,11 @@
& .Dropdown-menu { & .Dropdown-menu {
& > li > a { & > li > a {
padding: 8px 0 8px 30px; padding: 8px 0 8px 30px;
color: @muted-color; color: var(--sidenav-color, @muted-color);
&:hover { &:hover {
background: none; background: none;
color: @link-color; color: var(--sidenav-color-hover, @link-color);
text-decoration: none; text-decoration: none;
} }
@ -35,7 +35,7 @@
} }
& > li.active > a { & > li.active > a {
background: none; background: none;
color: @primary-color; color: var(--sidenav-color-active, @primary-color);
font-weight: bold; font-weight: bold;
} }
& > .Dropdown-separator { & > .Dropdown-separator {

View File

@ -1,8 +1,8 @@
.Hero { .Hero {
margin-top: -1px; margin-top: -1px;
background: @hero-bg; background: var(--hero-bg);
text-align: center; text-align: center;
color: @hero-color; color: var(--hero-color);
h2 { h2 {
margin: 0; margin: 0;

View File

@ -201,7 +201,7 @@
opacity: 0.5; opacity: 0.5;
} }
.Post-header .Button--more { .Post-header .Button--more {
.Button--color(@muted-more-color, fade(@muted-more-color, 30%)); .Button--color(@muted-more-color, fade(@muted-more-color, 30%), 'muted-more');
} }
} }
.Post--loading { .Post--loading {

View File

@ -1,5 +1,5 @@
.UserCard { .UserCard {
background: @control-bg; background: var(--usercard-bg);
.light-contents(); .light-contents();
background-size: 100% 100%; background-size: 100% 100%;
} }