Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(popover): add built-in translations #5436

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/popover/popover.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { newE2EPage } from "@stencil/core/testing";
import { html } from "../../../support/formatting";

import { accessible, defaults, hidden, renders, floatingUIOwner } from "../../tests/commonTests";
import { accessible, defaults, hidden, renders, floatingUIOwner, t9n } from "../../tests/commonTests";
import { CSS } from "./resources";

describe("calcite-popover", () => {
Expand All @@ -13,6 +13,8 @@ describe("calcite-popover", () => {
);
});

it("supports translations", () => t9n("calcite-popover"));

it("should have zIndex of 900", async () => {
const page = await newE2EPage();

Expand Down
70 changes: 58 additions & 12 deletions src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
ARIA_CONTROLS,
ARIA_EXPANDED,
HEADING_LEVEL,
TEXT,
defaultPopoverPlacement
} from "./resources";
import {
Expand Down Expand Up @@ -46,6 +45,15 @@ import { HeadingLevel, Heading } from "../functional/Heading";

import PopoverManager from "./PopoverManager";
import { debounce } from "lodash-es";
import { connectLocalized, disconnectLocalized, LocalizedComponent } from "../../utils/locale";
import {
connectMessages,
disconnectMessages,
setUpMessages,
T9nComponent,
updateMessages
} from "../../utils/t9n";
import { Messages } from "./assets/popover/t9n";

const manager = new PopoverManager();

Expand All @@ -55,9 +63,12 @@ const manager = new PopoverManager();
@Component({
tag: "calcite-popover",
styleUrl: "popover.scss",
shadow: true
shadow: true,
assetsDirs: ["assets"]
})
export class Popover implements FloatingUIComponent, OpenCloseComponent {
export class Popover
implements FloatingUIComponent, OpenCloseComponent, LocalizedComponent, T9nComponent
{
// --------------------------------------------------------------------------
//
// Properties
Expand Down Expand Up @@ -127,9 +138,34 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {
*/
@Prop({ reflect: true }) headingLevel: HeadingLevel;

/**
* Accessible name for the component's close button.
*
* @deprecated – translations are now built-in, if you need to override a string, please use `messageOverrides`
*/
@Prop() intlClose;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing type.


/** Accessible name for the component. */
@Prop() label!: string;

/**
* Use this property to override individual strings used by the component.
*/
@Prop({ mutable: true }) messageOverrides: Partial<Messages>;

@Watch("intlClose")
@Watch("messageOverrides")
onMessagesChange(): void {
/* wired up by t9n util */
}

/**
* Made into a prop for testing purposes only
*
* @internal
*/
@Prop({ mutable: true }) messages: Messages;

/**
* Offsets the position of the popover away from the `referenceElement`.
*
Expand Down Expand Up @@ -206,13 +242,6 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {
*/
@Prop({ reflect: true }) triggerDisabled = false;

/**
* Accessible name for the component's close button.
*
* @default "Close"
*/
@Prop() intlClose = TEXT.close;

// --------------------------------------------------------------------------
//
// Private Properties
Expand All @@ -223,8 +252,17 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {

@Element() el: HTMLCalcitePopoverElement;

@State() effectiveLocale = "";

@Watch("effectiveLocale")
effectiveLocaleChange(): void {
updateMessages(this, this.effectiveLocale);
}

@State() effectiveReferenceElement: ReferenceElement;

@State() defaultMessages: Messages;

arrowEl: HTMLDivElement;

closeButtonEl: HTMLCalciteActionElement;
Expand All @@ -245,6 +283,8 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {

connectedCallback(): void {
this.setFilteredPlacements();
connectLocalized(this);
connectMessages(this);
connectOpenCloseComponent(this);
const closable = this.closable || this.dismissible;
if (closable) {
Expand All @@ -256,6 +296,10 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {
this.setUpReferenceElement(this.hasLoaded);
}

async componentWillLoad(): Promise<void> {
await setUpMessages(this);
}

componentDidLoad(): void {
if (this.referenceElement && !this.effectiveReferenceElement) {
this.setUpReferenceElement();
Expand All @@ -266,6 +310,8 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {

disconnectedCallback(): void {
this.removeReferences();
disconnectLocalized(this);
disconnectMessages(this);
disconnectFloatingUI(this, this.effectiveReferenceElement, this.el);
disconnectOpenCloseComponent(this);
}
Expand Down Expand Up @@ -478,7 +524,7 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {
// --------------------------------------------------------------------------

renderCloseButton(): VNode {
const { closeButton, intlClose, heading, closable } = this;
const { closeButton, messages, heading, closable } = this;

return closable || closeButton ? (
<div class={CSS.closeButtonContainer}>
Expand All @@ -487,7 +533,7 @@ export class Popover implements FloatingUIComponent, OpenCloseComponent {
onClick={this.hide}
ref={(closeButtonEl) => (this.closeButtonEl = closeButtonEl)}
scale={heading ? "s" : "m"}
text={intlClose}
text={messages.close}
>
<calcite-icon icon="x" scale={heading ? "s" : "m"} />
</calcite-action>
Expand Down
4 changes: 0 additions & 4 deletions src/components/popover/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export const CSS = {
heading: "heading"
};

export const TEXT = {
close: "Close"
};

export const defaultPopoverPlacement = "auto";
export const ARIA_CONTROLS = "aria-controls";
export const ARIA_EXPANDED = "aria-expanded";
Expand Down
4 changes: 1 addition & 3 deletions src/utils/t9n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async function fetchMessages(component: T9nComponent, lang: string): Promise<Mes
*/
export async function updateMessages(component: T9nComponent, lang: string): Promise<void> {
component.defaultMessages = await fetchMessages(component, lang);
mergeMessages(component);
}

/**
Expand Down Expand Up @@ -139,8 +140,6 @@ export interface T9nComponent extends LocalizedComponent {

/**
* This property holds the component's default messages.
*
* This prop should use the `@State` decorator.
*/
defaultMessages: MessageBundle;

Expand All @@ -158,7 +157,6 @@ export interface T9nComponent extends LocalizedComponent {
*
* @Watch("intlMyPropA")
* @Watch("intlMyPropZ")
* @Watch("defaultMessages")
* @Watch("messageOverrides")
* onMessagesChange(): void {
* \/* wired up by t9n util *\/
Expand Down