Skip to content

Commit

Permalink
[1.x] Custom Colorising with CSS Custom Properties (#3001)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
SychO9 and davwheat authored Aug 16, 2021
1 parent 5798c4b commit ef20e29
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion js/src/admin/components/EditGroupModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class EditGroupModal extends Modal {
this.color() || this.icon()
? Badge.component({
icon: this.icon(),
style: { backgroundColor: this.color() },
color: this.color(),
})
: '',
' ',
Expand Down
5 changes: 4 additions & 1 deletion js/src/common/components/Badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ import classList from '../utils/classList';
*/
export default class Badge extends Component {
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 iconChild = iconName ? icon(iconName, { className: 'Badge-icon' }) : m.trust('&nbsp;');

const newStyle = { ...style, '--badge-bg': color };

const badgeAttrs = {
...attrs,
className,
style: newStyle,
};

const badgeNode = <div {...badgeAttrs}>{iconChild}</div>;
Expand Down
2 changes: 1 addition & 1 deletion js/src/common/components/GroupBadge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class GroupBadge extends Badge {

if (attrs.group) {
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.type = 'group--' + attrs.group.id();

Expand Down
2 changes: 1 addition & 1 deletion js/src/common/helpers/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function avatar(user: User, attrs: Object = {}): Mithril.Vnode {
}

content = username.charAt(0).toUpperCase();
attrs.style = { background: user.color() };
attrs.style = { '--avatar-bg': user.color() };
}

return <span {...attrs}>{content}</span>;
Expand Down
2 changes: 1 addition & 1 deletion js/src/forum/components/UserCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class UserCard extends Component {
const badges = user.badges().toArray();

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="container">
{controls.length
Expand Down
2 changes: 1 addition & 1 deletion less/admin/DashboardPage.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
margin-bottom: 20px;

.Button {
.Button--color(@control-color, @body-bg)
.Button--color(@control-color, @body-bg, 'button-alternate')
}
}

Expand Down
2 changes: 1 addition & 1 deletion less/common/Avatar.less
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
color: #fff;
text-align: center;
vertical-align: top;
background-color: @control-bg;
background-color: var(--avatar-bg);
font-weight: normal;
.Avatar--size(48px);

Expand Down
4 changes: 2 additions & 2 deletions less/common/Badge.less
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.Badge {
.Badge--size(22px);
background: @muted-color;
color: #fff;
background: var(--badge-bg);
color: var(--badge-color);
display: inline-block;
vertical-align: middle;
text-align: center;
Expand Down
20 changes: 11 additions & 9 deletions less/common/Button.less
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
padding: 8px 13px;
border-radius: @border-radius;
.user-select(none);
.Button--color(@control-color, @control-bg);
.Button--color(@control-color, @control-bg, 'button');
border: 0;

&:hover {
Expand Down Expand Up @@ -98,26 +98,28 @@
}
}

.Button--color(@color; @background) {
color: @color;
background: @background;
.Button--color(@color; @background; @name: 'button') {
color: var(~"--@{name}-color", ~"@{color}");
background: var(~"--@{name}-bg", ~"@{background}");
@bg-hover: darken(fadein(@background, 5%), 5%);
@bg-active: darken(fadein(@background, 10%), 10%);

&:hover,
&:focus,
&.focus {
background-color: darken(fadein(@background, 5%), 5%);
background-color: var(~"--@{name}-bg-hover", ~"@{bg-hover}");
}

&:active,
&.active,
.open > .Dropdown-toggle& {
background-color: darken(fadein(@background, 10%), 10%);
background-color: var(~"--@{name}-bg-active", ~"@{bg-active}");
}

&.disabled,
&[disabled],
fieldset[disabled] & {
background: @background;
background: var(~"--@{name}-bg-disabled", ~"@{background}");
}
}

Expand Down Expand Up @@ -166,7 +168,7 @@
}
}
.Button--primary {
.Button--color(@body-bg, @primary-color);
.Button--color(@body-bg, @primary-color, 'button-primary');
font-weight: bold;
padding-left: 20px;
padding-right: 20px;
Expand All @@ -176,7 +178,7 @@
}
}
.Button--danger {
.Button--color(@control-danger-color, @control-danger-bg);
.Button--color(@control-danger-color, @control-danger-bg, 'control-danger');
}
.Button--more {
padding: 2px 4px;
Expand Down
1 change: 1 addition & 0 deletions less/common/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@import "normalize";
@import "print";
@import "root";
@import "scaffolding";
@import "sideNav";

Expand Down
18 changes: 18 additions & 0 deletions less/common/root.less
Original file line number Diff line number Diff line change
@@ -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; }
}
11 changes: 0 additions & 11 deletions less/common/scaffolding.less
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 3 additions & 3 deletions less/common/sideNav.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
& .Dropdown-menu {
& > li > a {
padding: 8px 0 8px 30px;
color: @muted-color;
color: var(--sidenav-color, @muted-color);

&:hover {
background: none;
color: @link-color;
color: var(--sidenav-color-hover, @link-color);
text-decoration: none;
}

Expand All @@ -35,7 +35,7 @@
}
& > li.active > a {
background: none;
color: @primary-color;
color: var(--sidenav-color-active, @primary-color);
font-weight: bold;
}
& > .Dropdown-separator {
Expand Down
4 changes: 2 additions & 2 deletions less/forum/Hero.less
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.Hero {
margin-top: -1px;
background: @hero-bg;
background: var(--hero-bg);
text-align: center;
color: @hero-color;
color: var(--hero-color);

h2 {
margin: 0;
Expand Down
4 changes: 2 additions & 2 deletions less/forum/Post.less
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
opacity: 0.5;
}
.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 {
Expand Down Expand Up @@ -287,7 +287,7 @@
font-size: 14px;
margin-right: 5px;
}

&:empty {
display: none;
}
Expand Down
2 changes: 1 addition & 1 deletion less/forum/UserCard.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.UserCard {
background: @control-bg;
background: var(--usercard-bg);
.light-contents();
background-size: 100% 100%;
}
Expand Down

0 comments on commit ef20e29

Please sign in to comment.