Skip to content

Commit

Permalink
fix: add type definitions for translation labels
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed Aug 8, 2024
1 parent 2b9816b commit 3d12ec7
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import "yet-another-react-lightbox/styles.css";
<td>labels</td>
<td>object</td>
<td>
<p>Custom UI labels.</p>
<p>Custom UI labels / translations.</p>
<p>Example: <span class="font-mono">labels=&#123;&#123; Next: "Next slide" &#125;&#125;</span></p>
</td>
</tr>
Expand Down
3 changes: 2 additions & 1 deletion src/components/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import * as React from "react";
import { clsx, cssClass, label as translateLabel } from "../utils.js";
import { useLightboxProps } from "../contexts/index.js";
import { ELEMENT_BUTTON, ELEMENT_ICON } from "../consts.js";
import { Label } from "../types.js";

export type IconButtonProps = React.ComponentProps<"button"> & {
label: string;
label: Label;
icon: React.ElementType;
renderIcon?: () => React.ReactNode;
};
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";

import { ComponentProps, RenderFunction } from "../../types.js";
import { ComponentProps, Label, RenderFunction } from "../../types.js";
import { createModule } from "../../config.js";
import { useLoseFocus } from "../../hooks/index.js";
import { cssClass } from "../../utils.js";
Expand All @@ -11,7 +11,7 @@ import { useNavigationState } from "./useNavigationState.js";
import { useKeyboardNavigation } from "./useKeyboardNavigation.js";

export type NavigationButtonProps = {
label: string;
label: Label;
icon: React.ElementType;
renderIcon?: RenderFunction;
action: "prev" | "next";
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/captions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ declare module "../../types.js" {
buttonCaptions?: RenderFunction<CaptionsRef>;
}

interface Labels {
"Show captions"?: string;
"Hide captions"?: string;
}

/** Captions plugin ref */
interface CaptionsRef {
/** if `true`, captions are visible */
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/download/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ declare module "../../types.js" {
iconDownload?: RenderFunction;
}

interface Labels {
Download?: string;
}

// noinspection JSUnusedGlobalSymbols
interface Callbacks {
/** a callback called on slide download */
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/fullscreen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ declare module "../../types.js" {
iconExitFullscreen?: RenderFunction;
}

interface Labels {
// TODO v4: change Fullscreen to lowercase
"Enter Fullscreen"?: string;
"Exit Fullscreen"?: string;
}

// noinspection JSUnusedGlobalSymbols
interface Callbacks {
/** a callback called when the lightbox enters fullscreen mode */
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/share/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ declare module "../../types.js" {
iconShare?: RenderFunction;
}

interface Labels {
Share?: string;
}

// noinspection JSUnusedGlobalSymbols
interface Callbacks {
/** a callback called on slide share */
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/slideshow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ declare module "../../types.js" {
buttonSlideshow?: RenderFunction<SlideshowRef>;
}

interface Labels {
Play?: string;
Pause?: string;
}

// noinspection JSUnusedGlobalSymbols
interface Callbacks {
/** a callback called on slideshow playback start */
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/thumbnails/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ declare module "../../types.js" {
buttonThumbnails?: RenderFunction<ThumbnailsRef>;
}

interface Labels {
"Show thumbnails"?: string;
"Hide thumbnails"?: string;
}

/** `render.thumbnail` render function props */
type RenderThumbnailProps = {
slide: Slide;
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/zoom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ declare module "../../types.js" {
iconZoomOut?: RenderFunction;
}

interface Labels {
"Zoom in"?: string;
"Zoom out"?: string;
}

// noinspection JSUnusedGlobalSymbols
interface RenderSlideProps {
/** current zoom level */
Expand Down
14 changes: 9 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface LightboxProps {
slides: Slide[];
/** custom render functions */
render: Render;
/** custom UI labels */
/** custom UI labels / translations */
labels: Labels;
/** enabled plugins */
plugins: Plugin[];
Expand Down Expand Up @@ -377,10 +377,14 @@ export interface Callbacks {
exited?: Callback;
}

/** Custom UI labels */
export type Labels = {
[key: string]: string;
};
/** Custom UI labels / translations */
export interface Labels {
Previous?: string;
Next?: string;
Close?: string;
}

export type Label = keyof Labels;

/** Toolbar settings */
export interface ToolbarSettings {
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IMAGE_FIT_CONTAIN, IMAGE_FIT_COVER } from "./consts.js";
import {
CarouselSettings,
ContainerRect,
Label,
Labels,
LengthOrPercentage,
LightboxProps,
Expand Down Expand Up @@ -33,7 +34,7 @@ export function makeComposePrefix(base: string) {
return (prefix?: string) => composePrefix(base, prefix);
}

export function label(labels: Labels | undefined, defaultLabel: string) {
export function label(labels: Labels | undefined, defaultLabel: Label) {
return labels?.[defaultLabel] ?? defaultLabel;
}

Expand Down
19 changes: 19 additions & 0 deletions test/types/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ export default function App() {
thumbnailsTrack: { borderColor: "#eee" },
thumbnailsContainer: { borderColor: "#eee" },
}}
labels={{
Previous: "Custom Previous",
Next: "Custom Next",
Close: "Custom Close",
Play: "Custom Play",
Pause: "Custom Pause",
Share: "Custom Share",
Download: "Custom Download",
"Zoom in": "Custom Zoom In",
"Zoom out": "Custom Zoom Out",
"Enter Fullscreen": "Custom Enter Fullscreen",
"Exit Fullscreen": "Custom Exit Fullscreen",
"Show captions": "Custom Show Captions",
"Hide captions": "Custom Hide Captions",
"Show thumbnails": "Custom Show thumbnails",
"Hide thumbnails": "Custom Hide thumbnails",
// @ts-expect-error
"non-existent-label": "non-existent-label",
}}
/>

<button type="button" onClick={() => setOpen(true)} style={{ display: "block", margin: "20px auto" }}>
Expand Down

0 comments on commit 3d12ec7

Please sign in to comment.