Skip to content

Add new Theme Controller component - #31

Draft
Nittarab with Copilot wants to merge 7 commits into
mainfrom
copilot/fix-30
Draft

Add new Theme Controller component#31
Nittarab with Copilot wants to merge 7 commits into
mainfrom
copilot/fix-30

Conversation

Copilot AI commented Aug 5, 2025

Copy link
Copy Markdown
Contributor

This PR implements a new ThemeController component that provides comprehensive support for DaisyUI's theme switching functionality. The component follows the established patterns in the repository and supports all the theme controller variations found in DaisyUI.

Features

The ThemeController component supports multiple input types for theme switching:

  • Checkbox - Simple checkbox theme toggle
  • Toggle - Styled toggle switch with optional text labels and icons
  • Radio - Multiple radio inputs with labels in a fieldset
  • Radio Button - Radio inputs styled as buttons in a join group
  • Dropdown - Dropdown menu with theme selection options
  • Swap - Swap component with animated sun/moon icons using Phosphor icons
  • Toggle with Icons Inside - Special DaisyUI pattern with internal Phosphor icons

Usage Examples

<!-- Simple checkbox toggle -->
<%= render(DaisyUI::ThemeController.new(value: "dark")) %>

<!-- Toggle with sun/moon icons -->
<%= render(DaisyUI::ThemeController.new(
  type: :toggle,
  value: "dark",
  icon_before: helpers.sun_icon,
  icon_after: helpers.moon_icon
)) %>

<!-- Radio button selection -->
<%= render(DaisyUI::ThemeController.new(
  type: :radio_button,
  name: "theme-buttons",
  themes: [
    { value: "light", label: "Light" },
    { value: "dark", label: "Dark" },
    { value: "cyberpunk", label: "Cyberpunk" }
  ]
)) %>

<!-- Dropdown theme selector -->
<%= render(DaisyUI::ThemeController.new(
  type: :dropdown,
  button_text: "Choose Theme",
  themes: [
    { value: "light", label: "Light" },
    { value: "dark", label: "Dark" }
  ]
)) %>

Implementation Details

  • API Design: Follows existing component patterns with enum validation, system arguments support, and proper class computation
  • HTML Standards: Correctly handles boolean attributes (omits checked="false" rather than including it)
  • Icon System: Uses Phosphor icons consistently through the IconsHelper module, eliminating hardcoded SVG content
  • Unified Tag Helpers: Leverages Rails tag helper patterns with consolidated attribute building for maintainable code structure
  • Flexibility: Supports custom CSS classes, icons, text labels, and all DaisyUI theme controller patterns
  • Testing: Includes comprehensive preview examples with inline rendering and test coverage using PreviewTestConcern

The component generates HTML that exactly matches the expected DaisyUI theme controller patterns and includes a playground preview for interactive testing of all features. The implementation uses a unified approach with Rails tag helpers to minimize code duplication and improve maintainability.

Fixes #30.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: Nittarab <6181406+Nittarab@users.noreply.github.com>

@Nittarab Nittarab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot use the phospor icon, remove the SVG stuff

Co-authored-by: Nittarab <6181406+Nittarab@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new Theme Controller component Add new Theme Controller component Aug 5, 2025
Copilot AI requested a review from Nittarab August 5, 2025 14:55
Co-authored-by: Nittarab <6181406+Nittarab@users.noreply.github.com>

Copilot AI commented Aug 5, 2025

Copy link
Copy Markdown
Contributor Author

@copilot use the phospor icon, remove the SVG stuff

Replaced all hardcoded SVG icons with Phosphor icons. The ThemeController now uses sun_icon, moon_icon, and chevron_down_icon from the IconsHelper module, eliminating the inline SVG code. Changes made in commit a5aa92d.

@Nittarab

Nittarab commented Aug 6, 2025

Copy link
Copy Markdown
Owner

@copilot

run
/bin/fix_trailing_newlines.sh

then ensure the rails tests are passing

Co-authored-by: Nittarab <6181406+Nittarab@users.noreply.github.com>

Copilot AI commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

@copilot

run
...

Fixed trailing newlines in 13 files using ./bin/fix_trailing_newlines.sh and confirmed all Rails tests are passing (268 runs, 489 assertions, 0 failures). Also updated test fixtures to match the new Phosphor icon output. Changes committed in e3be34d.

@codecov

codecov Bot commented Aug 6, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.96954% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.51%. Comparing base (3bc28ae) to head (e3be34d).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...pp/components/daisy_ui/actions/theme_controller.rb 96.03% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #31      +/-   ##
==========================================
- Coverage   99.67%   99.51%   -0.17%     
==========================================
  Files          68       71       +3     
  Lines        1870     2067     +197     
  Branches      129      166      +37     
==========================================
+ Hits         1864     2057     +193     
- Misses          6       10       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Nittarab Nittarab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot
We can improve the approach of the theme controller component. Even if there are different previews with different types, we can use a tag.(type:) (check rails doc)
and paste the type of the tag and all the attributes that are currently manipulated for that type.

Comment thread .gitignore Outdated
/test/dummy/log/*.log
/test/dummy/storage/
/test/dummy/tmp/
/vendor/

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

discard this change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted the .gitignore change to remove the /vendor/ entry in commit f0ede5a.

Comment on lines +74 to +132
def theme_controller_using_a_checkbox
render_with_template
end

# Theme Controller using a Toggle
# ---------------
# Toggle switch for theme switching
def theme_controller_using_a_toggle
render_with_template
end

# Theme Controller using a Radio Input
# ---------------
# Radio inputs with labels for theme selection
def theme_controller_using_a_radio_input
render_with_template
end

# Theme Controller using a Radio Button
# ---------------
# Radio inputs styled as buttons
def theme_controller_using_a_radio_button
render_with_template
end

# Theme Controller using a Dropdown
# ---------------
# Dropdown menu with theme options
def theme_controller_using_a_dropdown
render_with_template
end

# Theme Controller using a Swap
# ---------------
# Swap component with sun/moon icons
def theme_controller_using_a_swap
render_with_template
end

# Theme Controller using a Toggle with Icons
# ---------------
# Toggle with sun and moon icons
def theme_controller_using_a_toggle_with_icons
render_with_template(locals: { helper: self })
end

# Theme Controller using a Toggle with Text
# ---------------
# Toggle with text labels before and after
def theme_controller_using_a_toggle_with_text
render_with_template
end

# Theme Controller using a Toggle with Custom Colors
# ---------------
# Toggle with custom Tailwind color classes
def theme_controller_using_a_toggle_with_custom_colors
render_with_template
end

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

There is no need to render with a template these previews render them inline

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Converted all preview methods from template-based rendering to inline component rendering and removed the unnecessary template files in commit f0ede5a.

Copilot AI and others added 2 commits August 6, 2025 12:52
Co-authored-by: Nittarab <6181406+Nittarab@users.noreply.github.com>
Co-authored-by: Nittarab <6181406+Nittarab@users.noreply.github.com>

Copilot AI commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

@copilot
We can improve the approach of the theme controller component. Even if there are different previews with different types, we can use a tag.(type:) (check rails doc)
and paste the type of t...

Improved the ThemeController component approach using Rails' tag helper more effectively in commit 2028fb1. Now uses unified tag.(type:) patterns with consolidated base_input_attributes, base_checkbox_attributes, and base_radio_attributes methods. This eliminates code duplication and provides a more maintainable structure while preserving all existing functionality.

Copilot AI requested a review from Nittarab August 6, 2025 12:57
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.

Add new Theme Controller component

2 participants