Skip to content

Commit

Permalink
Merge pull request #4 from coolusaHD/main
Browse files Browse the repository at this point in the history
Added the ability to assign an `id` to the underlying button
  • Loading branch information
todd-elvers committed Jan 9, 2023
2 parents f69298a + c2a258d commit 8702f92
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
20 changes: 10 additions & 10 deletions assets/index.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-dark-mode-toggle-2",
"version": "2.0.7",
"version": "2.0.8",
"description": "An updated, cute dark mode toggle button for React.",
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function LocalDevelopment() {
onClick={onToggleClick}
aria-hidden="true"
style={{ display: isLottiePlayerMounted ? "block" : "none" }}
id="dark-mode-toggle"
>
<LottiePlayerLight
loop={false}
Expand Down
1 change: 1 addition & 0 deletions src/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Template: Storybook.Story<Omit<DarkModeToggle.Props, "onChange">> = (props
<DarkModeToggle
{...props}
isDarkMode={isDarkMode}
id="dark-mode-toggle"
onChange={(isDarkMode) => {
action("onChange")({ isDarkMode });
console.log("onChange called:", { isDarkMode });
Expand Down
9 changes: 7 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ export declare namespace DarkModeToggle {

/** Optional className prop for the <button/> element (default = "") */
readonly className?: string;

/** Allow optional id prop */
readonly id?: string;
};
}

const lightToDarkSegment: AnimationSegment = [5, 50];
const darkToLightSegment: AnimationSegment = [50, 95];

export const DarkModeToggle = React.memo<DarkModeToggle.Props>(
({ isDarkMode, onChange, size = 85, speed = 1.3, className = "" }) => {
({ isDarkMode, onChange, size = 85, speed = 1.3, className = "", id = "" }) => {
const [sizeValue, sizeUnit] = parseUnit(size);
const [segments, setSegments] = React.useState<AnimationSegment>([0, 0]);
const [goTo] = React.useState(isDarkMode ? darkToLightSegment[0] : lightToDarkSegment[0]);
Expand All @@ -55,6 +58,7 @@ export const DarkModeToggle = React.memo<DarkModeToggle.Props>(
onClick={onToggleClick}
aria-hidden="true"
className={cx(buttonStyles(sizeValue, sizeUnit), className)}
id={id}
>
<LottiePlayerLight
className={playerStyles(isLottiePlayerMounted, sizeValue, sizeUnit)}
Expand All @@ -79,7 +83,8 @@ function arePropsEqual(prevProps: DarkModeToggle.Props, nextProps: DarkModeToggl
prevProps.size === nextProps.size &&
prevProps.isDarkMode === nextProps.isDarkMode &&
prevProps.speed === nextProps.speed &&
prevProps.className === nextProps.className
prevProps.className === nextProps.className &&
prevProps.id === nextProps.id
);
}

Expand Down

0 comments on commit 8702f92

Please sign in to comment.