From 959ca5f9f86ef2c0a5a23080cc01c25f53d613a9 Mon Sep 17 00:00:00 2001
From: Erika <3019731+Princesseuh@users.noreply.github.com>
Date: Fri, 8 Mar 2024 11:52:50 +0100
Subject: [PATCH] feat(toolbar): Allow every element to have every color
(#10186)
* feat(toolbar): Allow every element to have every color
* chore: changeset
* fix: add validatio
* fix: ok but what happened even
* nit: cooler messages
* fix: toggles
---
.changeset/khaki-elephants-hang.md | 5 +
.../runtime/client/dev-toolbar/settings.ts | 10 +-
.../client/dev-toolbar/ui-library/badge.ts | 120 +++++++++----
.../client/dev-toolbar/ui-library/button.ts | 163 ++++++++++++------
.../client/dev-toolbar/ui-library/card.ts | 72 +++++++-
.../dev-toolbar/ui-library/highlight.ts | 66 ++++++-
.../client/dev-toolbar/ui-library/toggle.ts | 76 +++++++-
7 files changed, 413 insertions(+), 99 deletions(-)
create mode 100644 .changeset/khaki-elephants-hang.md
diff --git a/.changeset/khaki-elephants-hang.md b/.changeset/khaki-elephants-hang.md
new file mode 100644
index 000000000000..fded6066008d
--- /dev/null
+++ b/.changeset/khaki-elephants-hang.md
@@ -0,0 +1,5 @@
+---
+"astro": minor
+---
+
+Adds the ability to set colors on all the included UI elements for dev toolbar apps. Previously, only badge and buttons could be customized.
diff --git a/packages/astro/src/runtime/client/dev-toolbar/settings.ts b/packages/astro/src/runtime/client/dev-toolbar/settings.ts
index ee7386d4f565..d031b636da1d 100644
--- a/packages/astro/src/runtime/client/dev-toolbar/settings.ts
+++ b/packages/astro/src/runtime/client/dev-toolbar/settings.ts
@@ -30,9 +30,9 @@ function getSettings() {
localStorage.setItem('astro:dev-toolbar:settings', JSON.stringify(_settings));
}
- function log(message: string) {
+ function log(message: string, level: 'log' | 'warn' | 'error' = 'log') {
// eslint-disable-next-line no-console
- console.log(
+ console[level](
`%cAstro`,
'background: linear-gradient(66.77deg, #D83333 0%, #F041FF 100%); color: white; padding-inline: 4px; border-radius: 2px; font-family: monospace;',
message
@@ -46,6 +46,12 @@ function getSettings() {
updateSetting,
logger: {
log,
+ warn: (message: string) => {
+ log(message, 'warn');
+ },
+ error: (message: string) => {
+ log(message, 'error');
+ },
verboseLog: (message: string) => {
if (_settings.verbose) {
log(message);
diff --git a/packages/astro/src/runtime/client/dev-toolbar/ui-library/badge.ts b/packages/astro/src/runtime/client/dev-toolbar/ui-library/badge.ts
index 9d10b65be27a..5d45f071bbe8 100644
--- a/packages/astro/src/runtime/client/dev-toolbar/ui-library/badge.ts
+++ b/packages/astro/src/runtime/client/dev-toolbar/ui-library/badge.ts
@@ -1,22 +1,53 @@
-type BadgeSize = 'small' | 'large';
-type BadgeStyle = 'purple' | 'gray' | 'red' | 'green' | 'yellow';
+import { settings } from '../settings.js';
+
+const sizes = ['small', 'large'] as const;
+const styles = ['purple', 'gray', 'red', 'green', 'yellow', 'blue'] as const;
+
+type BadgeSize = (typeof sizes)[number];
+type BadgeStyle = (typeof styles)[number];
export class DevToolbarBadge extends HTMLElement {
- size: BadgeSize = 'small';
- badgeStyle: BadgeStyle = 'purple';
+ _size: BadgeSize = 'small';
+ _badgeStyle: BadgeStyle = 'purple';
+
+ get size() {
+ return this._size;
+ }
+
+ set size(value) {
+ if (!sizes.includes(value)) {
+ settings.logger.error(
+ `Invalid size: ${value}, expected one of ${sizes.join(', ')}, got ${value}.`
+ );
+ return;
+ }
+ this._size = value;
+ this.updateStyle();
+ }
+
+ get badgeStyle() {
+ return this._badgeStyle;
+ }
+
+ set badgeStyle(value) {
+ if (!styles.includes(value)) {
+ settings.logger.error(
+ `Invalid style: ${value}, expected one of ${styles.join(', ')}, got ${value}.`
+ );
+ return;
+ }
+ this._badgeStyle = value;
+ this.updateStyle();
+ }
shadowRoot: ShadowRoot;
+ static observedAttributes = ['badge-style', 'size'];
+
constructor() {
super();
this.shadowRoot = this.attachShadow({ mode: 'open' });
- if (this.hasAttribute('size')) this.size = this.getAttribute('size') as BadgeSize;
-
- if (this.hasAttribute('badge-style'))
- this.badgeStyle = this.getAttribute('badge-style') as BadgeStyle;
-
- const classes = [`badge--${this.size}`, `badge--${this.badgeStyle}`];
this.shadowRoot.innerHTML = `
+
-
+
`;
}
+
+ connectedCallback() {
+ this.updateStyle();
+ }
+
+ attributeChangedCallback() {
+ if (this.hasAttribute('badge-style'))
+ this.badgeStyle = this.getAttribute('badge-style') as BadgeStyle;
+
+ if (this.hasAttribute('size')) this.size = this.getAttribute('size') as BadgeSize;
+ }
+
+ updateStyle() {
+ const style = this.shadowRoot.getElementById('selected-style') as HTMLStyleElement;
+
+ style.innerHTML = `
+ .badge {
+ --text-color: var(--${this.badgeStyle}-text);
+ --border-color: var(--${this.badgeStyle}-border);
+ --size: var(--${this.size});
+ }
+ `;
+ }
}
diff --git a/packages/astro/src/runtime/client/dev-toolbar/ui-library/button.ts b/packages/astro/src/runtime/client/dev-toolbar/ui-library/button.ts
index 3f625376d774..c63528c4447e 100644
--- a/packages/astro/src/runtime/client/dev-toolbar/ui-library/button.ts
+++ b/packages/astro/src/runtime/client/dev-toolbar/ui-library/button.ts
@@ -1,9 +1,46 @@
-type ButtonSize = 'small' | 'medium' | 'large';
-type ButtonStyle = 'ghost' | 'outline' | 'purple' | 'gray' | 'red';
+import { settings } from '../settings.js';
+
+const sizes = ['small', 'medium', 'large'] as const;
+const styles = ['ghost', 'outline', 'purple', 'gray', 'red', 'green', 'yellow', 'blue'] as const;
+
+type ButtonSize = (typeof sizes)[number];
+type ButtonStyle = (typeof styles)[number];
export class DevToolbarButton extends HTMLElement {
- size: ButtonSize = 'small';
- buttonStyle: ButtonStyle = 'purple';
+ _size: ButtonSize = 'small';
+ _buttonStyle: ButtonStyle = 'purple';
+
+ get size() {
+ return this._size;
+ }
+
+ set size(value) {
+ if (!sizes.includes(value)) {
+ settings.logger.error(
+ `Invalid size: ${value}, expected one of ${sizes.join(', ')}, got ${value}.`
+ );
+ return;
+ }
+ this._size = value;
+ this.updateStyle();
+ }
+
+ get buttonStyle() {
+ return this._buttonStyle;
+ }
+
+ set buttonStyle(value) {
+ if (!styles.includes(value)) {
+ settings.logger.error(
+ `Invalid style: ${value}, expected one of ${styles.join(', ')}, got ${value}.`
+ );
+ return;
+ }
+ this._buttonStyle = value;
+ this.updateStyle();
+ }
+
+ static observedAttributes = ['button-style', 'size'];
shadowRoot: ShadowRoot;
@@ -11,65 +48,63 @@ export class DevToolbarButton extends HTMLElement {
super();
this.shadowRoot = this.attachShadow({ mode: 'open' });
- if (this.hasAttribute('size')) this.size = this.getAttribute('size') as ButtonSize;
-
- if (this.hasAttribute('button-style'))
- this.buttonStyle = this.getAttribute('button-style') as ButtonStyle;
-
- const classes = [`button--${this.size}`, `button--${this.buttonStyle}`];
-
this.shadowRoot.innerHTML = `
+
-