Frontend Refactoring Practice for a Multimodal AI Search Application

    This article details the frontend refactoring of a multimodal AI search application (version 2.0), illustrating how the design system, performance optimization, and search interaction architecture work together. Assisted by AI programming, the process involved human oversight for requirement definition, code review, and outcome validation. The configurations and data discussed can serve as direct references, though specific performance gains must be remeasured in target projects. The article covers visual foundations, design token architecture, and the frontend architecture for multimodal search.

    This article uses the frontend refactoring of a multimodal AI search application version 2.0 as an example to illustrate how the design system, performance optimization, and search interaction architecture work together. The entire article was completed with AI assistance, while humans were responsible for requirement definition, code review, and outcome validation. The configurations and data mentioned here can be used as direct references, but specific gains need to be remeasured on target projects.

    1. Visual Foundations: Colors, Fonts, and Shadows

    The search application interface is the primary entry point for users to input queries, view results, and switch modalities. Version 2.0 adjusted the visual tone to a warm, bright style, aiming to reduce visual fatigue during prolonged use while ensuring clear information hierarchy.

    1.1 Color and Background Hierarchy

    The page uses a two-layer background. Pure white (#ffffff) is used for the results area and card bodies, while warm white (#f6f5f4) is used for the sidebar, bottom of the input area, and grouping background colors. The difference between the two backgrounds is minimal, so segmentation is achieved through background colors and thin borders rather than thick lines.

    Dividers use a semi-transparent black rgba(0, 0, 0, 0.1) instead of solid dark gray. This border is visible on a white background but does not draw attention away from the content itself.

    Brand blue #0075de serves as the primary interaction color, used for main buttons, links, and the currently selected modality tab. Contrast ratios need to be checked individually. Estimated using the WCAG formula:

    • White text on a #0075de button has a contrast ratio of approximately 4.56:1, meeting the AA standard for normal text (4.5:1).
    • 0075de text on a #f6f5f4 background has a contrast ratio of approximately 4.2:1, which does not meet the standard for normal body text, making it suitable only for large fonts or bold labels.

    • Secondary text #615d59 has a contrast ratio of about 6.5:1 on a white background and about 6.0:1 on #f6f5f4, meeting the standard on both backgrounds.

    The above values are approximate figures calculated manually using the formula. Before publishing, they should be reviewed using browser developer tools or contrast checking tools.

    Focus states require separate design. Keyboard users must be able to see the current focus position, so focus rings are defined for interactive elements and only displayed during keyboard navigation:

    1. :root {
    2. --focus-ring: 0 0 0 2px #ffffff, 0 0 0 4px var(--brand-blue);
    3. }
    4. :focus-visible {
    5. outline: none;
    6. box-shadow: var(--focus-ring);
    7. }

    Dark mode switches token values via attributes on the root node, so component code does not need modification:

    1. [data-theme="dark"] {
    2. --bg-primary: #1a1a1a;
    3. --bg-secondary: #242424;
    4. --text-primary: rgba(255, 255, 255, 0.92);
    5. --border-standard: rgba(255, 255, 255, 0.12);
    6. }

    The dark values above are just a starting point. The contrast ratio for each pair of foreground and background colors must be recalculated, and the performance of the brand blue on dark backgrounds should also be checked individually.

    1.2 System Font Stack

    Earlier versions loaded web fonts via third-party CDNs. This introduces two issues: the first screen must wait for fonts to download, and when fonts are not ready, text may experience FOUT (Flash of Unstyled Text) or FOIT (Flash of Invisible Text). Version 2.0 removed external fonts and switched to a system font stack:

    1. body {
    2. font-family:
    3. -apple-system,
    4. BlinkMacSystemFont,
    5. "SF Pro Text",
    6. "Segoe UI",
    7. "PingFang SC",
    8. "Microsoft YaHei",
    9. "Noto Sans SC",
    10. sans-serif;
    11. }

    The order of the font stack affects Chinese display. Both Segoe UI and SF Pro Text do not contain Chinese glyphs, so the browser will fall back to subsequent fonts one by one. Therefore, Chinese fonts should be placed after Western fonts, and it is best to include Noto Sans SC as a fallback for Linux and some Android devices. Otherwise, these systems might display box characters.

    Glyphs will differ across platforms, which is the trade-off of using system fonts. The team needs to accept these differences and check screenshots on real devices for titles, buttons, and long paragraph wrapping. If custom fonts are indeed needed later, use font-display: swap and preload the font files, while keeping the font size within necessary limits.

    1.3 Multi-layer Shadow Tokens

    Cards, popups, and dropdown panels use multi-layer shadows to simulate light attenuation, making floating layers stand out more than the page bottom. Shadows are managed uniformly using tokens:

    1. :root {
    2. --shadow-card:
    3. 0 1px 2px rgba(0, 0, 0, 0.04),
    4. 0 2px 4px rgba(0, 0, 0, 0.04),
    5. 0 4px 8px rgba(0, 0, 0, 0.04),
    6. 0 8px 16px rgba(0, 0, 0, 0.04);
    7. --shadow-popover:
    8. 0 2px 4px rgba(0, 0, 0, 0.06),
    9. 0 4px 8px rgba(0, 0, 0, 0.06),
    10. 0 8px 16px rgba(0, 0, 0, 0.06),
    11. 0 16px 32px rgba(0, 0, 0, 0.06);
    12. }

    Pay attention to performance when using them. The rendering cost of multi-layer shadows is higher than that of single-layer shadows. They are suitable for floating layers that are few in number and fixed in position, such as the hover state of search result cards, popups, and dropdown menus. Adding multi-layer shadows to every item in a list will significantly increase rendering time. During animations, only change transform and opacity, and avoid transitioning shadows.

    2. Design Tokens and CSS Variable Architecture

    Design tokens are a set of named values shared by design and development. Their purpose is to reduce hardcoding and allow unified modifications to colors, spacing, and border radii.

    2.1 Token Layering and Naming

    Tokens are divided into two layers. The primitive layer only records original values, such as --blue-600: #0075de. The semantic layer records usage, such as --color-action-primary: var(--blue-600). Components only reference the semantic layer. This way, when the brand color changes, only the primitive layer values need to be modified.

    The naming conventions are as follows:

    • Colors are named by “category-role-state”, such as --color-text-secondary.
    • Spacing is numbered in 4px units, such as --space-2 representing 8px.
    • Border radii are named by size, such as --radius-md.
    • Do not use names describing position or appearance like “primary blue” or “large button color”, as they will become invalid after theme switching.

    2.2 8px Main Grid and 4px Half Grid

    The spacing system uses an 8px main grid. Layout spacing, card padding, and block spacing use multiples of 8. Odd multiples of 4px are only used for fine-tuning within components, such as the spacing between icons and text, and padding for badges.

    The tokens below are numbered in 4px units and indicate which belong to the main grid:

    1. :root {
    2. /* 4px half grid: only for fine-tuning within components */
    3. --space-1: 4px;
    4. --space-3: 12px;
    5. --space-5: 20px;
    6. /* 8px main grid: layout and block spacing */
    7. --space-2: 8px;
    8. --space-4: 16px;
    9. --space-6: 24px;
    10. --space-8: 32px;
    11. /* Border radius */
    12. --radius-sm: 4px;
    13. --radius-md: 8px;
    14. --radius-lg: 12px;
    15. }

    Components should reference tokens like this:

    1. .result-card {
    2. padding: var(--space-4);
    3. border-radius: var(--radius-lg);
    4. background: var(--bg-primary);
    5. box-shadow: var(--shadow-card);
    6. }

    The validation checklist for theme switching is as follows:

    1. After switching to dark mode, the contrast ratio of all text and backgrounds still meets the AA standard.
    2. After modifying --brand-blue, buttons, links, and selected states change synchronously without any residual hardcoded colors.
    3. Traverse the entire page via keyboard; every interactive element displays a focus ring.
    4. Search panels, result cards, and popups have clear boundaries in both themes.
    5. Use a style inspection script to search for hexadecimal colors and pixel values in the source code to confirm that new code does not bypass tokens.

    The visual and performance layers address how the interface is presented. Multimodal search also needs to handle the relationship between input, requests, result merging, and rendering. The following explains the responsibilities of each layer according to the data flow.

    3.1 Input Parsing Layer

    Users can input text, upload images, or record audio. Each type of input must first be converted into a unified request object, which subsequent modules will only process:

    1. type Modality = "text" | "image" | "audio";
    2. interface SearchRequest {
    3. id: string;
    4. createdAt: number;
    5. parts: Array<{ type: Modality; payload: string | Blob }>;
    6. filters: Record<string, string>;
    7. }

    Validation must be performed at the input boundary:

    • Text length is limited to 500 characters; if exceeded, it is truncated and the user is notified.
    • Images only accept `image…