Skip to content

Commit

Permalink
Custom button cleanup & toolbar button styles (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
emma-sg authored May 21, 2024
1 parent e853b62 commit 040d1b7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 49 deletions.
52 changes: 36 additions & 16 deletions frontend/src/components/ui/button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable lit/binding-positions */
/* eslint-disable lit/no-invalid-html */
import clsx from "clsx";
import { css } from "lit";
import { customElement, property } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";
Expand All @@ -8,6 +9,8 @@ import { html, literal } from "lit/static-html.js";
import { TailwindElement } from "@/classes/TailwindElement";
import { tw } from "@/utils/tailwind";

type Variant = "neutral" | "danger";

/**
* Custom styled button
*
Expand All @@ -21,8 +24,18 @@ export class Button extends TailwindElement {
@property({ type: String })
type: "submit" | "button" = "button";

// TODO unify button styling & variants - there are probably a few different
// approaches for difference button use cases, but we'll figure that out in
// the future when we work more on our UI library.
// See also https://github.com/webrecorder/browsertrix/issues/1550
@property({ type: String })
variant: "primary" | "danger" | "neutral" = "neutral";
variant: Variant = "neutral";

@property({ type: Boolean })
filled = false;

@property({ type: String })
size: "small" | "medium" = "medium";

@property({ type: String })
label?: string;
Expand All @@ -39,9 +52,6 @@ export class Button extends TailwindElement {
@property({ type: Boolean })
loading = false;

@property({ type: Boolean })
icon = false;

static styles = css`
:host {
display: inline-block;
Expand All @@ -57,18 +67,28 @@ export class Button extends TailwindElement {
const tag = this.href ? literal`a` : literal`button`;
return html`<${tag}
type=${this.type === "submit" ? "submit" : "button"}
class=${[
tw`flex h-6 cursor-pointer items-center justify-center gap-2 rounded-sm text-center font-medium transition-all disabled:cursor-not-allowed disabled:text-neutral-300`,
this.icon ? tw`min-h-8 min-w-8 px-1` : tw`h-6 px-2`,
this.raised ? tw`shadow-sm` : "",
{
primary: tw`bg-blue-50 text-blue-600 shadow-blue-800/20 hover:bg-blue-100`,
danger: tw`shadow-danger-800/20 bg-danger-50 text-danger-600 hover:bg-danger-100`,
neutral: tw`text-neutral-600 hover:text-blue-600`,
}[this.variant],
]
.filter(Boolean)
.join(" ")}
class=${clsx(
tw`flex h-6 cursor-pointer items-center justify-center gap-2 text-center font-medium outline-3 outline-offset-1 outline-primary transition focus-visible:outline disabled:cursor-not-allowed disabled:text-neutral-300`,
this.size === "medium"
? tw`min-h-8 min-w-8 rounded-sm`
: tw`min-h-6 min-w-6 rounded-md`,
this.raised && tw`border shadow-sm`,
this.filled
? [
tw`text-white`,
{
neutral: tw`border-primary-800 bg-primary-500 shadow-primary-800/20 hover:bg-primary-600`,
danger: tw`shadow-danger-800/20 border-danger-800 bg-danger-500 hover:bg-danger-600`,
}[this.variant],
]
: [
this.raised && tw`bg-white`,
{
neutral: tw`border-gray-300 text-gray-600 hover:text-primary-600`,
danger: tw`shadow-danger-800/20 border-danger-300 bg-danger-50 text-danger-600 hover:bg-danger-100`,
}[this.variant],
],
)}
?disabled=${this.disabled}
href=${ifDefined(this.href)}
aria-label=${ifDefined(this.label)}
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/components/ui/copy-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ export class CopyButton extends LitElement {
@sl-hide=${this.stopProp}
@sl-after-hide=${this.stopProp}
>
<sl-icon-button
name=${this.isCopied ? "check-lg" : this.name ? this.name : "copy"}
label=${msg("Copy to clipboard")}
<btrix-button
size="small"
@click=${this.onClick}
?disabled=${!this.value && !this.getValue}
></sl-icon-button>
class="inline"
raised
>
<sl-icon
name=${this.isCopied ? "check-lg" : this.name ? this.name : "copy"}
label=${msg("Copy to clipboard")}
></sl-icon>
</btrix-button>
</sl-tooltip>
`;
}
Expand Down
35 changes: 19 additions & 16 deletions frontend/src/components/ui/copy-field.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { localized } from "@lit/localize";
import clsx from "clsx";
import { css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { customElement, property, queryAssignedNodes } from "lit/decorators.js";

import { TailwindElement } from "@/classes/TailwindElement";
import { tw } from "@/utils/tailwind";

/**
* Copy text to clipboard on click
Expand Down Expand Up @@ -49,26 +50,27 @@ export class CopyField extends TailwindElement {
}
`;

get _slottedChildren() {
const slot = this.shadowRoot?.querySelector("slot[name=label]");
return (slot as HTMLSlotElement | null | undefined)?.assignedElements();
}
@queryAssignedNodes({ slot: "label" })
private readonly slottedChildren: Element[] | undefined;

render() {
return html`
<div role="group">
<div
role="group"
class=${clsx(
tw`rounded border`,
this.filled && tw`bg-slate-50`,
this.monostyle && tw`font-monostyle`,
)}
>
<label
class="${classMap({
hidden: !this.label && !this._slottedChildren,
})} mb-1.5 inline-block font-sans text-xs leading-[1.4] text-neutral-800"
class="${clsx(
"mb-1.5 inline-block font-sans text-xs leading-[1.4] text-neutral-800",
!this.label && !this.slottedChildren?.length && tw`hidden`,
)} "
><slot name="label">${this.label}</slot></label
>
<div
class="${classMap({
"bg-slate-50": this.filled,
"font-monostyle": this.monostyle,
})} relative inline-flex w-full items-stretch justify-start rounded border"
>
<div class="relative inline-flex w-full items-stretch justify-start">
<slot name="prefix"></slot>
<span
aria-hidden=${this.hideContentFromScreenReaders}
Expand All @@ -82,6 +84,7 @@ export class CopyField extends TailwindElement {
.content=${this.buttonContent}
.getValue=${this.getValue}
.hoist=${this.hoist}
class="m-1 flex"
></btrix-copy-button>
</div>
</div>
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/features/crawl-workflows/queue-exclusion-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,10 @@ export class QueueExclusionForm extends LiteElement {
: ""}
</sl-input>
</div>
<div class="flex-0 w-10 pt-1 text-center">
<div class="flex-0 w-10 text-center">
<btrix-button
variant="primary"
variant="neutral"
raised
icon
?disabled=${!this.inputValue ||
this.isRegexInvalid ||
this.isSubmitting}
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/pages/org/archived-item-qa/archived-item-qa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,11 +872,14 @@ export class ArchivedItemQA extends TailwindElement {
placement="bottom-start"
>
<btrix-button
icon
variant=${!this.splitView ? "primary" : "neutral"}
raised
?filled=${!this.splitView}
size="small"
@click="${() => (this.splitView = !this.splitView)}"
class="m-0.5"
aria-pressed=${!this.splitView}
>
<sl-icon name="vr" label=${msg("Split view")}></sl-icon>
<sl-icon name="vr"></sl-icon>
</btrix-button>
</sl-tooltip>
</div>
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/pages/org/collection-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,14 @@ export class CollectionDetail extends LiteElement {
class="mb-2"
.value="${publicReplayUrl}"
hideContentFromScreenReaders
hoist
>
<sl-tooltip slot="prefix" content=${msg("Open in New Tab")}>
<sl-tooltip slot="prefix" content=${msg("Open in New Tab")} hoist>
<sl-icon-button
href=${publicReplayUrl}
name="box-arrow-up-right"
target="_blank"
class="m-px"
>
</sl-icon-button>
</sl-tooltip>
Expand All @@ -323,12 +325,11 @@ export class CollectionDetail extends LiteElement {
</p>
<div class="relative mb-5 rounded border bg-slate-50 p-3 pr-9">
<btrix-code value=${embedCode}></btrix-code>
<div
class="absolute right-1.5 top-1.5 rounded border bg-white shadow-sm"
>
<div class="absolute right-1 top-1">
<btrix-copy-button
.getValue=${() => embedCode}
content=${msg("Copy Embed Code")}
hoist
></btrix-copy-button>
</div>
</div>
Expand All @@ -340,12 +341,11 @@ export class CollectionDetail extends LiteElement {
</p>
<div class="relative mb-5 rounded border bg-slate-50 p-3 pr-9">
<btrix-code language="javascript" value=${importCode}></btrix-code>
<div
class="absolute right-1.5 top-1.5 rounded border bg-white shadow-sm"
>
<div class="absolute right-1 top-1">
<btrix-copy-button
.getValue=${() => importCode}
content=${msg("Copy JS")}
hoist
></btrix-copy-button>
</div>
</div>
Expand Down

0 comments on commit 040d1b7

Please sign in to comment.