Skip to content

Conversation

@M-ayank2005
Copy link
Contributor

Resolves #1153

Description:

This PR improves the ModeToggle component by optimizing theme persistence and enhancing UI behavior. The changes ensure a seamless dark mode experience.

Key Changes:

ModeToggle Component:

  • Optimized the theme toggle logic for better user experience.
  • Enhanced button styling and transitions for a smoother UI experience.

Why This is Needed:

  • Ensures a better user experience by respecting system dark mode preferences.

Testing & Validation:

✔️ Verified UI updates across different screen sizes.
✔️ Ensured localStorage properly persists theme selection.
✔️ Checked TypeScript type safety for component props.
✔️ Tested theme switching behavior with system preferences.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 21, 2025

Summary by CodeRabbit

  • New Features

    • Revamped the mode toggle control to provide a more engaging theme switching experience with a prominent, rounded button and dynamic icons reflecting the active mode.
    • Introduced smooth background transitions and enhanced hover effects to improve visual feedback.
  • Refactor

    • Streamlined the layout for a cleaner and more intuitive user interface.

Walkthrough

This pull request refactors the dark/light mode toggle component by replacing a checkbox input wrapped in a label with a button element. The button directly triggers the darkModeHandler function on click and features a redesigned visual style, including a rounded shape, shadow effect, background transitions, and hover effects. The icon inside the button updates based on the current theme—displaying faMoon for light mode and faSun for dark mode—and its size is increased. The outer layout is simplified by removing the extra spacing class.

Changes

File Change Summary
frontend/src/components/ModeToggle.tsx Replaced checkbox and label with a button element triggering darkModeHandler; updated visual styling (rounded shape, shadow, color transitions, hover effects); increased icon size and swapped icons based on theme; removed unnecessary spacing class.

Assessment against linked issues

Objective Addressed Explanation
Ensure correct toggle state for dark/light mode (#1153) UI redesign changes are implemented, but initial state logic is unclear.
Update logo based on selected theme (#1153) No changes were made regarding logo update functionality.

Suggested reviewers

  • kasya
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
frontend/src/components/ModeToggle.tsx (2)

20-25: Consider optimizing dark mode state management.

The current implementation toggles the dark class in both the useEffect hook (lines 12-18) and the darkModeHandler function (line 23). This creates redundant DOM operations.

const darkModeHandler = () => {
  setDark(!dark)
  const newTheme = !dark ? 'dark' : 'light'
- document.body.classList.toggle('dark', !dark)
  localStorage.setItem('theme', newTheme)
}

The useEffect hook will handle the class toggling when the state changes, so there's no need to do it in the handler as well.


8-10: Consider adding system preference detection.

The current implementation only checks localStorage but doesn't respect the user's system preference for dark/light mode when they first visit the site.

const [dark, setDark] = useState(() => {
-  return localStorage.getItem('theme') === 'dark'
+  const storedTheme = localStorage.getItem('theme')
+  if (storedTheme) {
+    return storedTheme === 'dark'
+  }
+  return window.matchMedia('(prefers-color-scheme: dark)').matches
})

This would respect the user's system preference if they haven't explicitly set a theme yet.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e1e138a and b5c99a0.

📒 Files selected for processing (1)
  • frontend/src/components/ModeToggle.tsx (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
frontend/src/components/ModeToggle.tsx (1)
frontend/src/utils/utility.ts (1) (1)
  • cn (15-17)
🔇 Additional comments (4)
frontend/src/components/ModeToggle.tsx (4)

28-28: Refined layout structure.

The removal of the space-x-2 class from the flex container is appropriate since the toggle is now a single button component rather than multiple elements needing spacing.


36-50: Excellent UI/UX improvements for the mode toggle.

The replacement of the checkbox with a proper button element significantly improves:

  1. Accessibility - proper semantic HTML with aria-label
  2. Visual feedback - smooth transitions, hover and active states
  3. User experience - intuitive icon representation (showing what you'll switch to)
  4. Keyboard navigation - proper focus states

The layered div approach creates a polished interactive effect while maintaining clean functionality.


44-44: Improved icon logic.

Inverting the icon logic (!dark ? faMoon : faSun) is more intuitive - now showing users what they'll switch to rather than the current state. This better aligns with user expectations.


38-38: Comprehensive button styling - good practice.

The button styling is comprehensive with appropriate transitions, focus states, and dark mode variants. The use of active:scale-95 provides excellent tactile feedback without being distracting.

Copy link
Collaborator

@arkid15r arkid15r left a comment

Choose a reason for hiding this comment

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

Thanks for addressing this! Please look into the following comments when you get a chance:

</label>
<button
onClick={darkModeHandler}
className="relative h-10 w-10 transform overflow-hidden rounded-full border border-gray-300 shadow-sm transition-all duration-200 hover:shadow-md focus:outline-none focus:ring-1 focus:ring-gray-600 active:scale-95 dark:border-gray-600"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you make it look more simple? It seems there is unnecessary border when the button is in focus. Also for the light theme the background seems inconsistent comparing to what we have for the dark theme behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I've made the requested changes. I have made the toggle button match with the other button such as sponser to have a consistent ui also i have applied same hover effect as in other buttons . Please review and let me know if any further modifications are needed.

@arkid15r arkid15r requested a review from aramattamara March 21, 2025 23:34
@M-ayank2005
Copy link
Contributor Author

@arkid15r I've made the requested changes. I have made the toggle button match with the other button such as sponser and star to have a consistent ui also i have applied same hover effect as in other buttons . Please review and let me know if any further modifications are needed.

@arkid15r arkid15r enabled auto-merge March 22, 2025 18:34
Copy link
Collaborator

@arkid15r arkid15r left a comment

Choose a reason for hiding this comment

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

LGTM

@sonarqubecloud
Copy link

@arkid15r arkid15r added this pull request to the merge queue Mar 22, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
frontend/src/components/ModeToggle.tsx (1)

20-25: Consider simplifying the theme toggle logic to avoid redundancy.

The darkModeHandler function contains potentially redundant operations. Line 23 directly toggles the 'dark' class on the document body, but this is already handled by the useEffect hook on lines 12-18 that reacts to changes in the dark state.

  const darkModeHandler = () => {
    setDark(!dark)
    const newTheme = !dark ? 'dark' : 'light'
-   document.body.classList.toggle('dark', !dark)
    localStorage.setItem('theme', newTheme)
  }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 17ed8b7 and 3371996.

📒 Files selected for processing (1)
  • frontend/src/components/ModeToggle.tsx (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
frontend/src/components/ModeToggle.tsx (1)
frontend/src/utils/utility.ts (1) (1)
  • cn (15-17)
🔇 Additional comments (3)
frontend/src/components/ModeToggle.tsx (3)

36-48: Great UI improvement with consistent button styling.

The button implementation is a significant improvement over the previous checkbox approach. The styling now matches other UI elements like the sponsor and star buttons as requested in the previous review. The hover effects, transitions, and focus states create a polished user experience.


43-44: Icon sizing and transitions look good.

The icon now displays correctly based on the theme state (sun for dark mode, moon for light mode) and includes appropriate sizing and transition effects. The additional hover rotation effect adds a nice touch to the user experience.


28-28: Simplified container styling.

Removing the space-x-2 class makes sense since there's now only a single button element within the container rather than multiple elements requiring spacing.

Merged via the queue into OWASP:main with commit 15c6f8b Mar 22, 2025
15 of 16 checks passed
@M-ayank2005 M-ayank2005 deleted the feature branch March 25, 2025 17:50
shdwcodr pushed a commit to shdwcodr/Nest that referenced this pull request Jun 5, 2025
* fix: dark mode toggle

* Updated code based on review suggestions

* Update code

---------

Co-authored-by: Arkadii Yakovets <2201626+arkid15r@users.noreply.github.com>
Co-authored-by: Arkadii Yakovets <arkadii.yakovets@owasp.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dark Mode Toggle Needs Better UX Behavior

2 participants