Skip to content

feat(tip,tip-manager): add built-in translations #6074

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

Merged
merged 7 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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/tip-manager/tip-manager.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { newE2EPage } from "@stencil/core/testing";
import { CSS, TEXT } from "./resources";
import { accessible, defaults, renders, hidden } from "../../tests/commonTests";
import { accessible, defaults, renders, hidden, t9n } from "../../tests/commonTests";

describe("calcite-tip-manager", () => {
it("renders", async () => renders("calcite-tip-manager", { display: "block" }));
Expand Down Expand Up @@ -239,4 +239,6 @@ describe("calcite-tip-manager", () => {

expect(heading.tagName).toEqual("H2");
});

it("supports translations", () => t9n("calcite-tip-manager"));
});
44 changes: 1 addition & 43 deletions src/components/tip-manager/tip-manager.stories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { boolean, text } from "@storybook/addon-knobs";
import { boolean } from "@storybook/addon-knobs";
import {
Attribute,
filterComponentAttributes,
Expand All @@ -7,7 +7,6 @@ import {
themesDarkDefault
} from "../../../.storybook/utils";
import readme from "./readme.md";
import { TEXT } from "./resources";
import { html } from "../../../support/formatting";
import { placeholderImage } from "../../../.storybook/placeholderImage";
import { storyFilters } from "../../../.storybook/helpers";
Expand All @@ -30,47 +29,6 @@ const createAttributes: (options?: { exceptions: string[] }) => Attributes = ({
delete this.build;
return this;
}
},

{
name: "intl-close",
commit(): Attribute {
this.value = text("intlClose", TEXT.close);
delete this.build;
return this;
}
},
{
name: "intl-default-title",
commit(): Attribute {
this.value = text("intlDefaultTitle", TEXT.defaultGroupTitle);
delete this.build;
return this;
}
},
{
name: "intl-pagination-label",
commit(): Attribute {
this.value = text("intlPaginationLabel", TEXT.defaultPaginationLabel);
delete this.build;
return this;
}
},
{
name: "intl-next",
commit(): Attribute {
this.value = text("intlNext", TEXT.next);
delete this.build;
return this;
}
},
{
name: "intl-previous",
commit(): Attribute {
this.value = text("intlPrevious", TEXT.previous);
delete this.build;
return this;
}
}
],
exceptions
Expand Down
83 changes: 52 additions & 31 deletions src/components/tip-manager/tip-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@ import {
h,
VNode
} from "@stencil/core";
import { CSS, ICONS, TEXT } from "./resources";
import { CSS, ICONS } from "./resources";
import { getElementDir, toAriaBoolean } from "../../utils/dom";
import { HeadingLevel, Heading } from "../functional/Heading";
import { createObserver } from "../../utils/observers";
import { Messages } from "./assets/tip-manager/t9n";
import {
connectMessages,
disconnectMessages,
setUpMessages,
updateMessages
} from "../../utils/t9n";
import { connectLocalized, disconnectLocalized } from "../../utils/locale";

/**
* @slot - A slot for adding `calcite-tip`s.
*/
@Component({
tag: "calcite-tip-manager",
styleUrl: "tip-manager.scss",
shadow: true
shadow: true,
assetsDirs: ["assets"]
})
export class TipManager {
// --------------------------------------------------------------------------
Expand All @@ -45,29 +54,21 @@ export class TipManager {
@Prop({ reflect: true }) headingLevel: HeadingLevel;

/**
* Accessible name for the component's close button.
*/
@Prop() intlClose: string;

/**
* Accessible name for the `calcite-tip-group` title.
*/
@Prop() intlDefaultTitle: string;

/**
* Accessible name for navigating to the next tip.
* Made into a prop for testing purposes only
*
* @internal
*/
@Prop() intlNext: string;
@Prop({ mutable: true }) messages: Messages;

/**
* Text that accompanies the component's pagination.
* Use this property to override individual strings used by the component.
*/
@Prop() intlPaginationLabel: string;
@Prop({ mutable: true }) messageOverrides: Partial<Messages>;

/**
* Accessible name for navigating to the previous tip.
*/
@Prop() intlPrevious: string;
@Watch("messageOverrides")
onMessagesChange(): void {
/* wired up by t9n util */
}

// --------------------------------------------------------------------------
//
Expand Down Expand Up @@ -97,19 +98,38 @@ export class TipManager {

container: HTMLDivElement;

@State() defaultMessages: Messages;

@State() effectiveLocale = "";

@Watch("effectiveLocale")
async effectiveLocaleChange(): Promise<void> {
await updateMessages(this, this.effectiveLocale);
this.updateGroupTitle();
}

// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------

connectedCallback(): void {
connectLocalized(this);
connectMessages(this);
this.setUpTips();
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}

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

disconnectedCallback(): void {
this.mutationObserver?.disconnect();
disconnectLocalized(this);
disconnectMessages(this);
}

// --------------------------------------------------------------------------
Expand Down Expand Up @@ -162,11 +182,10 @@ export class TipManager {
this.tips = tips;
this.selectedIndex = selectedTip ? tips.indexOf(selectedTip) : 0;

tips.forEach((tip) => {
tips.forEach((tip: HTMLCalciteTipElement) => {
tip.closeDisabled = true;
});
this.showSelectedTip();
this.updateGroupTitle();
}

hideTipManager = (): void => {
Expand All @@ -183,9 +202,11 @@ export class TipManager {
}

updateGroupTitle(): void {
const selectedTip = this.tips[this.selectedIndex];
const tipParent = selectedTip.closest("calcite-tip-group");
this.groupTitle = tipParent?.groupTitle || this.intlDefaultTitle || TEXT.defaultGroupTitle;
if (this.tips) {
const selectedTip = this.tips[this.selectedIndex];
const tipParent = selectedTip.closest("calcite-tip-group");
this.groupTitle = tipParent?.groupTitle || this.messages?.defaultGroupTitle;
}
}

previousClicked = (): void => {
Expand Down Expand Up @@ -233,11 +254,11 @@ export class TipManager {

renderPagination(): VNode {
const dir = getElementDir(this.el);
const { selectedIndex, tips, total, intlNext, intlPrevious, intlPaginationLabel } = this;
const { selectedIndex, tips, total, messages } = this;

const nextLabel = intlNext || TEXT.next;
const previousLabel = intlPrevious || TEXT.previous;
const paginationLabel = intlPaginationLabel || TEXT.defaultPaginationLabel;
const nextLabel = messages.next;
const previousLabel = messages.previous;
const paginationLabel = messages.defaultPaginationLabel;

return tips.length > 1 ? (
<footer class={CSS.pagination}>
Expand All @@ -261,9 +282,9 @@ export class TipManager {
}

render(): VNode {
const { closed, direction, headingLevel, groupTitle, selectedIndex, intlClose, total } = this;
const { closed, direction, headingLevel, groupTitle, selectedIndex, messages, total } = this;

const closeLabel = intlClose || TEXT.close;
const closeLabel = messages.close;

if (total === 0) {
return null;
Expand Down
4 changes: 0 additions & 4 deletions src/components/tip/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,3 @@ export const ICONS = {
export const SLOTS = {
thumbnail: "thumbnail"
};

export const TEXT = {
close: "Close"
};
4 changes: 3 additions & 1 deletion src/components/tip/tip.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { newE2EPage } from "@stencil/core/testing";
import { accessible, hidden, renders, defaults, slots } from "../../tests/commonTests";
import { accessible, hidden, renders, defaults, slots, t9n } from "../../tests/commonTests";
import { CSS, SLOTS } from "./resources";

describe("calcite-tip", () => {
Expand Down Expand Up @@ -64,4 +64,6 @@ describe("calcite-tip", () => {

expect(header).not.toBeNull();
});

it("supports translations", () => t9n("calcite-tip"));
});
9 changes: 0 additions & 9 deletions src/components/tip/tip.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from "../../../.storybook/utils";
import readme from "./readme.md";
import groupReadme from "../tip-group/readme.md";
import { TEXT } from "./resources";
import { placeholderImage } from "../../../.storybook/placeholderImage";
import { storyFilters } from "../../../.storybook/helpers";

Expand Down Expand Up @@ -46,14 +45,6 @@ const createAttributes: (options?: { exceptions: string[] }) => Attributes = ({
delete this.build;
return this;
}
},
{
name: "intl-close",
commit(): Attribute {
this.value = text("intlClose", TEXT.close);
delete this.build;
return this;
}
}
],
exceptions
Expand Down
Loading