Material Design 3 Implementation Guide

· 个人乱谈

This document provides guidance on how the Material Design 3 (MD3) principles have been implemented in our blog frontend project, and how to use these components when developing new features.

Table of Contents

  1. Theme System
  2. Responsive Layout System
  3. Components
  4. Motion and Animation
  5. Best Practices

Theme System

Dynamic Color

We've implemented dynamic color support following the Material You philosophy. This allows the theme to adapt to user preferences and featured content.

// Example usage of dynamic color in article component
import { useTheme } from '../../contexts/ThemeContext';

const ArticleDetail: React.FC<ArticleDetailProps> = ({ 
  article,
  /* other props */ 
}) => {
  const { applyDynamicColor } = useTheme();
  
  useEffect(() => {
    // Apply dynamic color based on article's featured image
    if (article?.imageUrl) {
      applyDynamicColor(article.imageUrl);
    }
  }, [article?.imageUrl, applyDynamicColor]);
  
  // ...component implementation
};

Theme Context

The ThemeContext provides utilities for theme management throughout the app:

const { 
  themeSettings,     // Current theme settings object
  isDark,            // Boolean indicating if dark mode is active
  changeThemeMode,   // Function to change theme mode (light, dark, auto)
  enableDynamicColor, // Function to toggle dynamic color
  changePrimaryColor, // Function to set a specific primary color
  applyDynamicColor  // Function to apply dynamic color from an image
} = useTheme();

CSS Variables

All components use CSS variables defined in theme.css for colors, typography, and other design tokens:

.my-component {
  background-color: var(--md-sys-color-surface-container);
  color: var(--md-sys-color-on-surface);
  padding: var(--md-sys-spacing-4);
}

Responsive Layout System

Window Size Classes

We follow the MD3 window size classes to create responsive layouts:

  • Compact (<600dp): Mobile phones, small foldables
  • Medium (600dp-839dp): Tablets, large foldables
  • Expanded (≥840dp): Desktops, large tablets
import useResponsive from '../../hooks/useResponsive';

const MyComponent = () => {
  const { isCompact, isMedium, isExpanded } = useResponsive();
  
  return (
    <div>
      {isCompact && <MobileView />}
      {!isCompact && <DesktopView />}
    </div>
  );
};

Adaptive Layout Component

The AdaptiveLayout component manages the overall page structure and adapts to different screen sizes:

<AdaptiveLayout 
  title="Article Title" 
  showNavigation={true}
  showFooter={true}
  showSearch={true}
  sideContent={<RelatedArticles />}
>
  <ArticleContent />
</AdaptiveLayout>

Components

AdaptiveNavigation

The AdaptiveNavigation component automatically transforms between bottom navigation bar on mobile and side navigation rail on tablet/desktop:

<AdaptiveNavigation items={[
  {
    id: 'home',
    label: 'Home',
    icon: 'home',
    href: '/'
  },
  // Additional navigation items...
]} />

EnhancedArticleCard

The EnhancedArticleCard implements M3 card principles with different variants and layouts:

<EnhancedArticleCard
  id={article.id}
  title={article.title}
  excerpt={article.excerpt}
  /* Other article props */
  variant="elevated" // 'elevated', 'filled', or 'outlined'
  layout="standard" // 'compact', 'standard', or 'featured'
  enableColorExtraction={true}
  onClick={handleArticleClick}
/>

ThemeToggle

The ThemeToggle component allows users to switch between light, dark, and auto theme modes, as well as toggle dynamic color:

<ThemeToggle size="medium" />

Motion and Animation

Container Transform

Use the ContainerTransform component for seamless transitions between UI elements, such as expanding an article card into a full article detail page:

const [isDetailOpen, setIsDetailOpen] = useState(false);
const [selectedArticle, setSelectedArticle] = useState(null);
const [originElement, setOriginElement] = useState(null);

const handleArticleClick = (article, event) => {
  setSelectedArticle(article);
  setOriginElement(event.currentTarget);
  setIsDetailOpen(true);
};

// In render function
<>
  {articles.map(article => (
    <EnhancedArticleCard
      key={article.id}
      {...article}
      onClick={(id) => handleArticleClick(
        article, 
        document.getElementById(`article-${id}`)
      )}
    />
  ))}
  
  <ContainerTransform
    isOpen={isDetailOpen}
    onClose={() => setIsDetailOpen(false)}
    originElement={originElement}
  >
    {selectedArticle && <ArticleDetail article={selectedArticle} />}
  </ContainerTransform>
</>

Animation Best Practices

All animations follow M3's principles:

  1. Emphasized Transitions: For container transforms and prominent UI changes

    transition: transform 0.3s var(--md-sys-motion-easing-emphasized);
    
  2. Standard Transitions: For most UI elements

    transition: opacity 0.2s var(--md-sys-motion-easing-standard);
    
  3. Interactive States: Use state layers for hover, focus, and pressed states

    .my-button:hover::before {
      opacity: 0.08; /* M3 hover state layer opacity */
    }
    

Best Practices

Colors

  • Use semantic color tokens (e.g., primary, on-primary) instead of specific color values
  • Use surface container variants (surface-container-low, surface-container-high, etc.) for layering UI elements
  • Apply state layer colors correctly for interactive elements

Typography

  • Use the type scale consistently (md-typescale-headline-small, md-typescale-body-medium, etc.)
  • Follow the mapping in the design docs for content types (e.g., headline-small for card titles)

Components

  • Select appropriate component variants based on information hierarchy
  • Use elevated components for important actions, outlined or filled for secondary elements
  • Follow the M3 component anatomy (e.g., cards should have proper spacing and structure)

Responsive Design

  • Use the proper layout for each window size class
  • Adapt component density and presentation based on available space
  • Follow the "reflow" pattern rather than just scaling content

Accessibility

  • Ensure adequate color contrast (MD3 default tokens are designed for this)
  • Provide proper focus indicators and keyboard navigation
  • Include appropriate ARIA attributes on custom components

This guide is a living document and will be updated as our MD3 implementation evolves.