web: refactor status label to separate component (#7407)

* web: break circular dependency between AKElement & Interface.

This commit changes the way the root node of the web application shell is
discovered by child components, such that the base class shared by both
no longer results in a circular dependency between the two models.

I've run this in isolation and have seen no failures of discovery; the identity
token exists as soon as the Interface is constructed and is found by every item
on the page.

* web: fix broken typescript references

This built... and then it didn't?  Anyway, the current fix is to
provide type information the AkInterface for the data that consumers
require.

* A quality of life thing: `<ak-status-label good>`

There's an idiom throughout the UI:

``` HTML
<ak-label color=${item.enabled ? PFColor.Green : PFColor.Red}>
      ${item.enabled ? msg("Yes") : msg("No")}
      </ak-label>
```

There are two problems with this.

- Repeating the conditional multiple times is error-prone
- The color scheme doesn't communicate much.

There are uses for ak-label that aren't like this, but I'm focusing on this particular use case,
which occurs about 20 times throughout the UI.

Since it's so common, let's isolate the most common case: `<ak-status-label good />` gives you the
"good" status, and `<ak-status-label/>` gives you the "bad" status, which is the default (no
arguments to the function).

There wasn't much clarity in the system for when to use orange vs red vs grey, but looking through
the use cases, it became clear that Red meant fail/inaccessible, Orange meant "Warning, but not
blocking," and Grey just means "info: this thing is off".

So let's define that with meaning: there are three types, error, warning, and info. Which
corresponds to debugging levels, but whatever, nerds grok that stuff.

So that example at the top becomes

```<ak-status-label ?good=${item.enabled}></ak-status-label>```

... and we can now more clearly understand what that conveys.

There is some heavy tension in this case: this is an easier and quicker-to-write solution to
informing the user of a binary status in an iconic way, but the developer has to remember that it
exists.

Story provided, and changes to the existing uses of the existing idiom provided.

* Added the 'compact label' story to storybook.
This commit is contained in:
Ken Sternberg
2023-11-20 11:24:48 -08:00
committed by GitHub
parent bb52765f51
commit 73751e5cd9
22 changed files with 277 additions and 92 deletions

View File

@ -0,0 +1,116 @@
import { AKElement } from "@goauthentik/elements/Base";
import { msg } from "@lit/localize";
import { css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import PFLabel from "@patternfly/patternfly/components/Label/label.css";
import PFBase from "@patternfly/patternfly/patternfly-base.css";
const statusNames = ["error", "warning", "info"] as const;
type StatusName = (typeof statusNames)[number];
const statusToDetails = new Map<StatusName, [string, string]>([
["error", ["pf-m-red", "fa-times"]],
["warning", ["pf-m-orange", "fa-exclamation-triangle"]],
["info", ["pf-m-gray", "fa-info-circle"]],
]);
const styles = css`
:host {
--pf-c-label--m-gray--BackgroundColor: var(--pf-global--palette--black-100);
--pf-c-label--m-gray__icon--Color: var(--pf-global--primary-color--100);
--pf-c-label--m-gray__content--Color: var(--pf-global--info-color--200);
--pf-c-label--m-gray__content--before--BorderColor: var(--pf-global--palette--black-400);
--pf-c-label--m-gray__content--link--hover--before--BorderColor: var(
--pf-global--primary-color--100
);
--pf-c-label--m-gray__content--link--focus--before--BorderColor: var(
--pf-global--primary-color--100
);
}
.pf-c-label.pf-m-gray {
--pf-c-label--BackgroundColor: var(--pf-c-label--m-gray--BackgroundColor);
--pf-c-label__icon--Color: var(--pf-c-label--m-gray__icon--Color);
--pf-c-label__content--Color: var(--pf-c-label--m-gray__content--Color);
--pf-c-label__content--before--BorderColor: var(
--pf-c-label--m-gray__content--before--BorderColor
);
--pf-c-label__content--link--hover--before--BorderColor: var(
--pf-c-label--m-gray__content--link--hover--before--BorderColor
);
--pf-c-label__content--link--focus--before--BorderColor: var(
--pf-c-label--m-gray__content--link--focus--before--BorderColor
);
}
`;
/**
* A boolean status indicator
*
* Based on the Patternfly "label" pattern, this component exists to display "Yes" or "No", but this
* is configurable.
*
* When the boolean attribute `good` is present, the background will be green and the icon will be a
* ✓. If the `good` attribute is not present, the background will be a warning color and an
* alternative symbol. Which color and symbol depends on the `type` of the negative status we want
* to show:
*
* - type="error" (default): A Red ✖
* - type="warning" An orange ⚠
* - type="info" A grey ⓘ
*
* By default, the messages for "good" and "other" are "Yes" and "No" respectively, but these can be
* customized with the attributes `good-label` and `bad-label`.
*/
@customElement("ak-status-label")
export class AkStatusLabel extends AKElement {
static get styles() {
return [PFBase, PFLabel, styles];
}
@property({ type: Boolean })
good = false;
@property({ type: String, attribute: "good-label" })
goodLabel = msg("Yes");
@property({ type: String, attribute: "bad-label" })
badLabel = msg("No");
@property({ type: Boolean })
compact = false;
@property({ type: String })
type: StatusName = "error";
render() {
const details = statusToDetails.get(this.type);
if (!details) {
throw new Error(`Bad status type [${this.type}] passed to ak-status-label`);
}
const [label, color, icon] = this.good
? [this.goodLabel, "pf-m-green", "fa-check"]
: [this.badLabel, ...details];
const classes = {
"pf-c-label": true,
[color]: true,
"pf-m-compact": this.compact,
};
return html`<span class="${classMap(classes)}">
<span class="pf-c-label__content">
<span class="pf-c-label__icon">
<i class="fas fa-fw ${icon}" aria-hidden="true"></i> </span
>${label}
</span>
</span>`;
}
}
export default AkStatusLabel;

View File

@ -0,0 +1,101 @@
import "@goauthentik/elements/messages/MessageContainer";
import { Meta } from "@storybook/web-components";
import { TemplateResult, html } from "lit";
import "../ak-status-label";
import AkStatusLabel from "../ak-status-label";
const metadata: Meta<AkStatusLabel> = {
title: "Components / App Status Label",
component: "ak-status-label",
parameters: {
docs: {
description: {
component: "A status label display",
},
},
},
};
export default metadata;
const container = (testItem: TemplateResult) =>
html` <div style="background: #fff; padding: 2em">
<style>
dl {
display: grid;
grid-template-columns: 22ch 1fr;
gap: 0.5rem;
}
</style>
${testItem}
</div>`;
export const AppIcon = () => {
// prettier-ignore
return container(html`
<dl>
<dt>Good</dt><dd>
<ak-status-label good></ak-status-label>
</dd>
<dt>Bad (Default)</dt><dd>
<ak-status-label></ak-status-label>
</dd>
<dt>Programmatically Good</dt><dd>
<ak-status-label ?good=${true}></ak-status-label>
</dd>
<dt>Programmatically Bad</dt><dd>
<ak-status-label ?good=${false}></ak-status-label>
</dd>
<dt>Good Warning</dt><dd>
<ak-status-label type="warning" good></ak-status-label>
</dd>
<dt>Bad Warning</dt><dd>
<ak-status-label type="warning"></ak-status-label>
</dd>
<dt>Good Info</dt><dd>
<ak-status-label type="info" good></ak-status-label>
</dd>
<dt>Bad Info</dt><dd>
<ak-status-label type="info"></ak-status-label>
</dd>
<dt>Good With Alternative Message</dt><dd>
<ak-status-label good good-label="Hurray!" bad-label="Boo!"></ak-status-label>
</dd>
<dt>Bad with Alternative Message</dt><dd>
<ak-status-label good-label="Hurray!" bad-label="Boo!"></ak-status-label>
</dd>
<dt>Good, Compact</dt><dd>
<ak-status-label good compact></ak-status-label>
</dd>
<dt>Bad, Compact</dt><dd>
<ak-status-label compact></ak-status-label>
</dd>
</dl>
`);
};