Skip to content
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

Add Theme Toggle / Dark Mode #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

trance128
Copy link

@trance128 trance128 commented Jul 26, 2024

Add shadcn's Theme Toggle and support for darkmode.

Also, menu has been updated with the border-border class, which is needed to use the correctly colored border in dark mode

Summary by Sourcery

This pull request adds a Theme Toggle component to enable dark mode support in Phoenix LiveView applications. It also updates the menu component for proper border coloring in dark mode and includes comprehensive documentation and tests for the new feature.

  • New Features:
    • Introduced a Theme Toggle component to support dark mode in Phoenix LiveView applications.
  • Enhancements:
    • Updated the menu component to include the 'border-border' class for proper border coloring in dark mode.
  • Documentation:
    • Added documentation for the new Theme Toggle component, including usage instructions, customization options, and examples.
  • Tests:
    • Updated tests for the menu component to reflect the addition of the 'border-border' class.

Copy link

sourcery-ai bot commented Jul 26, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new Theme Toggle component to support dark mode in the application. The README.md file has been updated with detailed instructions on how to use the new component. The menu component has been modified to include a 'border-border' class for proper border color in dark mode, and corresponding tests have been updated. Additionally, the new ThemeToggle module has been added with implementation details and customization options.

File-Level Changes

Files Changes
README.md
lib/salad_ui/theme_toggle.ex
Added documentation and implementation for the new Theme Toggle component, including usage instructions and customization options.
lib/salad_ui/menu.ex
test/salad_ui/menu_test.exs
Updated menu component and corresponding tests to include 'border-border' class for dark mode support.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @trance128 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a brief example of how to use the Theme Toggle component in the main README.md file for quick reference.
Here's what I looked at during the review
  • 🟡 General issues: 4 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟡 Documentation: 2 issues found

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@@ -0,0 +1,186 @@
defmodule SaladUI.ThemeToggle do
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider moving JavaScript code to a separate file

The JavaScript code for theme switching is currently included in the module documentation. Moving this to a separate .js file would improve maintainability and allow for easier testing.

Suggested change
defmodule SaladUI.ThemeToggle do
defmodule SaladUI.ThemeToggle do
@moduledoc """
Implementation of Theme Toggle from https://ui.shadcn.com/docs/dark-mode/next
"""
@external_resource "assets/js/theme_toggle.js"
end

attr :class, :string, default: ""
attr :on_theme_change, :any, default: "set_theme", doc: "A string (event name) or 1-arity function to handle theme changes. Omit to use default js functions"

def theme_toggle(assigns) do
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider breaking down the theme_toggle function

The theme_toggle function is quite long and handles multiple responsibilities. Breaking it down into smaller, more focused functions could improve readability and maintainability.

Suggested change
def theme_toggle(assigns) do
def theme_toggle(assigns) do
~H"""
<div class={@class}>
"""
end
def render_theme_toggle(assigns) do
theme_toggle(assigns)
end

on_theme_change.(theme)
end

defp sun_icon(assigns) do
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider moving icon functions to a separate module

If the sun_icon and moon_icon functions are used elsewhere in the application, consider moving them to a separate icons module for better organization and reusability.

"""
end

defp theme_change_event(theme, on_theme_change) when is_binary(on_theme_change) do
Copy link

Choose a reason for hiding this comment

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

suggestion: Add error handling for theme change events

Consider adding error handling for cases where on_theme_change is neither a string nor a function. This would improve the robustness of the component.

Suggested change
defp theme_change_event(theme, on_theme_change) when is_binary(on_theme_change) do
defp theme_change_event(theme, on_theme_change) when is_binary(on_theme_change) do
JS.dispatch(on_theme_change, detail: %{theme: theme})
end
defp theme_change_event(_theme, _on_theme_change) do
{:error, "Invalid on_theme_change value"}
end


3. Implement theme switching functionality: You can either use the provided JavaScript or implement your own theme switching logic.

Using the Provided JavaScript
Copy link

Choose a reason for hiding this comment

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

suggestion (documentation): Inconsistent title case.

The phrase 'Using the Provided JavaScript' should be in title case for consistency, i.e., 'Using the provided JavaScript'.

Suggested change
Using the Provided JavaScript
Using the provided JavaScript

});
```

Using Custom Theme Switching Logic
Copy link

Choose a reason for hiding this comment

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

suggestion (documentation): Inconsistent title case.

The phrase 'Using Custom Theme Switching Logic' should be in title case for consistency, i.e., 'Using custom theme switching logic'.

Suggested change
Using Custom Theme Switching Logic
Using custom theme switching logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant