DynamicShapeStyle

The DynamicShapeStyle type allows you to define two distinct styles for a shape—one for light mode and another for dark mode. The system automatically applies the appropriate style based on the current color scheme (light or dark) of the user’s device.

Overview

Dynamic styling is a critical aspect of creating adaptive and visually appealing user interfaces. With DynamicShapeStyle, you can ensure that your shapes blend seamlessly with the user's preferred color scheme by defining separate styles for light and dark modes.

Key points:

  • Define a style for light mode using the light property.
  • Define a style for dark mode using the dark property.
  • The system will automatically apply the appropriate style based on the user's current settings.

Declaration

1type DynamicShapeStyle = {
2    light: ShapeStyle;
3    dark: ShapeStyle;
4};
  • light: ShapeStyle
    The style to apply when the system is in light mode.

  • dark: ShapeStyle
    The style to apply when the system is in dark mode.

Supported ShapeStyle

The ShapeStyle can be a color, gradient, or material. For example:

  • Colors: Solid colors like "red", hex values like "#FF0000", or CSS-like RGBA strings like "rgba(255, 0, 0, 1)".
  • Gradients: Linear or radial gradients.
  • Materials: System materials like "regularMaterial", "thickMaterial".

Example Usage

Using Dynamic Colors

1const dynamicStyle: DynamicShapeStyle = {
2  light: "blue",
3  dark: "gray"
4}
5
6<Text
7  foregroundStyle={dynamicStyle}
8/>

In this example, the shape appears blue in light mode and gray in dark mode.

Using Dynamic Gradients

1const dynamicStyle: DynamicShapeStyle = {
2  light: {
3    gradient: [
4      { color: "lightblue", location: 0 },
5      { color: "white", location: 1 }
6    ],
7    startPoint: { x: 0, y: 0 },
8    endPoint: { x: 1, y: 1 }
9  },
10  dark: {
11    gradient: [
12      { color: "darkblue", location: 0 },
13      { color: "black", location: 1 }
14    ],
15    startPoint: { x: 0, y: 0 },
16    endPoint: { x: 1, y: 1 }
17  }
18}
19
20<Circle
21  fill={dynamicStyle}
22/>

Here, the shape uses a light blue to white gradient in light mode and a dark blue to black gradient in dark mode.

Using Materials

1const dynamicStyle: DynamicShapeStyle = {
2  light: "regularMaterial",
3  dark: "ultraThickMaterial"
4}
5
6<HStack
7  background={dynamicStyle}
8></HStack>

This configuration applies a regular material in light mode and an ultra-thick material in dark mode.

Why Use DynamicShapeStyle?

Dynamic styling helps create a better user experience by ensuring:

  1. Visual Harmony: Your shapes adapt to the user's color scheme, maintaining aesthetic coherence.
  2. Accessibility: Adjusting styles for dark mode improves readability and usability in low-light environments.
  3. Consistency: Aligns your app's appearance with system-wide preferences, making it feel more integrated.

Summary

With DynamicShapeStyle, you can create flexible and adaptive styles for shapes that seamlessly respond to the user's color scheme. By defining separate styles for light and dark modes, your app will look great in any environment, ensuring a consistent and user-friendly experience.