Skip to content

Commit

Permalink
feat(counter): add custom separator (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjeltenmarabous authored May 19, 2023
1 parent 2944efb commit 5da0e54
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/plugins/counter/Counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ import {
PluginProps,
useLightboxState,
} from "../../index.js";
import { resolveCounterProps } from "./props.js";

export function CounterComponent({ counter: { className, ...rest } = {} }: ComponentProps) {
export function CounterComponent({ counter }: ComponentProps) {
const { slides, currentIndex } = useLightboxState();

const {
separator,
container: { className, ...rest },
// TODO v4: remove legacy configuration options
className: legacyClassName,
...legacyRest
} = resolveCounterProps(counter);

if (slides.length === 0) return null;

return (
<div className={clsx(cssClass("counter"), className)} {...rest}>
{currentIndex + 1} / {slides.length}
<div className={clsx(cssClass("counter"), className || legacyClassName)} {...legacyRest} {...rest}>
{currentIndex + 1} {separator} {slides.length}
</div>
);
}

/** Counter plugin */
export function Counter({ addChild }: PluginProps) {
export function Counter({ augment, addChild }: PluginProps) {
augment(({ counter, ...restProps }) => ({
counter: resolveCounterProps(counter),
...restProps,
}));

addChild(MODULE_CONTROLLER, createModule(PLUGIN_COUNTER, CounterComponent));
}
10 changes: 8 additions & 2 deletions src/plugins/counter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { Counter } from "./Counter.js";

declare module "../../types.js" {
interface LightboxProps {
/** HTML div element attributes to be passed to the Counter plugin container */
counter?: React.HTMLAttributes<HTMLDivElement>;
// TODO v4: remove html attributes from `counter` prop
/** Counter plugin settings */
counter?: React.HTMLAttributes<HTMLDivElement> & {
/** custom separator */
separator?: string;
/** counter container HTML attributes */
container?: React.HTMLAttributes<HTMLDivElement>;
};
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/plugins/counter/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LightboxProps } from "../../index.js";

export const defaultCounterProps = {
separator: "/",
container: {},
} as Required<NonNullable<LightboxProps["counter"]>>;

export const resolveCounterProps = (counter: LightboxProps["counter"]) => ({
...defaultCounterProps,
...counter,
});

0 comments on commit 5da0e54

Please sign in to comment.