Skip to content

refactor: replace isDark conditional logic with Tailwind dark: prefix across codebase#225

Merged
lyzno1 merged 8 commits intomainfrom
copilot/fix-220-2
Aug 14, 2025
Merged

refactor: replace isDark conditional logic with Tailwind dark: prefix across codebase#225
lyzno1 merged 8 commits intomainfrom
copilot/fix-220-2

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 13, 2025

This PR systematically refactors the codebase to replace runtime isDark conditional logic with Tailwind's static dark: prefix classes, following official Tailwind CSS best practices for dark mode implementation.

Problem

The codebase was using runtime conditional logic throughout components for dark mode theming:

// Problematic pattern - runtime overhead
const { isDark } = useTheme();
className={isDark ? 'bg-stone-800 text-stone-100' : 'bg-white text-stone-900'}

This approach created several issues:

  • Performance overhead: Every component using useTheme() hook adds runtime checks
  • Bundle size impact: Dynamic class generation prevents Tailwind's tree-shaking optimization
  • Maintenance complexity: Conditional logic makes components harder to read and debug
  • Framework compliance: Not following Tailwind CSS official best practices

Solution

Replaced conditional styling with static Tailwind dark: classes:

// Best practice - zero runtime overhead
className="bg-white text-stone-900 dark:bg-stone-800 dark:text-stone-100"

Changes Made

Successfully refactored 68 isDark usages across multiple component categories:

Skeleton Components

  • sidebar-chat-skeleton.tsx - Removed isDark prop, converted to static classes
  • chat-skeleton.tsx - Eliminated conditional logic for loading states

File Preview System

  • file-preview-canvas.tsx - Comprehensive refactor of all internal components:
    • LoadingSkeleton, ErrorDisplay, FileInfoFallback
    • Removed useTheme dependency from main component
    • Updated component interfaces to remove isDark props

UI Components

  • nav-button.tsx - Converted hover and text styling
  • search-input.tsx - Refactored complex input styling with focus states
  • typing-dots.tsx - Simple color transformation for animated elements
  • more-button.tsx - Complex component with array-based conditional styling
  • scroll-to-bottom-button.tsx - Removed unused isDark destructuring
  • return-to-chat-button.tsx - Border and background state styling
  • page-loading-spinner.tsx - Spinner icon color theming

Advanced Patterns Handled

Complex Focus States:

// Before
isDark
  ? 'border-stone-600 bg-stone-700 focus:ring-stone-500'  
  : 'border-stone-300 bg-stone-50 focus:ring-stone-400'

// After  
'border-stone-300 bg-stone-50 focus:ring-stone-400 dark:border-stone-600 dark:bg-stone-700 dark:focus:ring-stone-500'

Array-based Conditional Logic:

// Before
isDark ? ['text-gray-400', isActive && 'bg-stone-600'] : ['text-gray-500', isActive && 'bg-gray-100']

// After
'text-gray-500 dark:text-gray-400',
isActive && 'bg-gray-100 dark:bg-stone-600'

Core Infrastructure

The foundation was already in place - Tailwind v4 dark mode configuration in globals.css:

@variant dark (&:where(.dark, .dark *));

This enables the CSS-only theme switching that makes this refactoring possible.

Benefits

Zero runtime overhead - Pure CSS theme switching
Better tree-shaking - Tailwind can properly analyze static classes
Improved maintainability - Static class names are easier to debug
Standards compliance - Follows official Tailwind dark mode patterns
Type safety - Reduces complex conditional logic

Testing

  • All changes pass TypeScript compilation (pnpm type-check)
  • Systematic validation after each component refactor
  • Theme switching functionality preserved (CSS-based, no JS changes needed)

Remaining Work

This PR addresses the foundational components and establishes patterns for the remaining 1618 isDark usages. The systematic approach taken here provides a clear blueprint for future refactoring of more complex components.

Addressing #220.


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

Copilot AI and others added 3 commits August 14, 2025 00:08
… in skeleton and file-preview components

Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com>
Copilot AI changed the title [WIP] refactor: replace isDark conditional logic with Tailwind dark: prefix across codebase refactor: replace isDark conditional logic with Tailwind dark: prefix across codebase Aug 14, 2025
Copilot AI requested a review from lyzno1 August 14, 2025 00:19
@lyzno1 lyzno1 marked this pull request as ready for review August 14, 2025 00:26
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. area:ui UI components, layouts, styling, accessibility. type:refactor Internal code improvements without behavior change. labels Aug 14, 2025
Comment thread components/sidebar/chat-skeleton.tsx Outdated
Comment thread components/sidebar/sidebar-chat-skeleton.tsx Outdated
Copy link
Copy Markdown
Member

@lyzno1 lyzno1 left a comment

Choose a reason for hiding this comment

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

@copilot please use pnpm format or pnpm lint-staged to ensure ci workflows success.

Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Aug 14, 2025

@copilot please use pnpm format or pnpm lint-staged to ensure ci workflows success.

Applied Prettier formatting to ensure CI workflow success. All formatting checks now pass. Commit: c4df7e2

Copy link
Copy Markdown
Member

@lyzno1 lyzno1 left a comment

Choose a reason for hiding this comment

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

@copilot please ensure all git diff files do not use isDark, I see any files still use this.

Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Aug 14, 2025

@copilot please ensure all git diff files do not use isDark, I see any files still use this.

Completed removal of all isDark usages from git diff files. The user-table.tsx file had 37 remaining instances that have now been converted to static Tailwind dark: prefix classes, including complex conditional logic in dropdown menus, table headers, row styling, and button states. All files in the PR now use zero-runtime-overhead CSS-based theme switching. Commit: 80c0e4e

Copilot AI requested a review from lyzno1 August 14, 2025 01:03
@lyzno1 lyzno1 requested a review from zhangxuhe1 August 14, 2025 01:14
@dosubot dosubot Bot added the lgtm Looks good to me; approved by a reviewer. label Aug 14, 2025
@lyzno1 lyzno1 merged commit d75d9bd into main Aug 14, 2025
14 checks passed
@lyzno1 lyzno1 deleted the copilot/fix-220-2 branch August 14, 2025 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ui UI components, layouts, styling, accessibility. lgtm Looks good to me; approved by a reviewer. size:L This PR changes 100-499 lines, ignoring generated files. type:refactor Internal code improvements without behavior change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants