sailfork / t3code-github / apps/ mobile/ src/ components/ ui/ FloatingActionButton.android.tsx

import type { FloatingActionButtonProps } from "./FloatingActionButton.types";
import { NativeThemeHost } from "./NativeThemeHost.android";
import {
  Box,
  ExtendedFloatingActionButton,
  FloatingActionButton as ComposeFloatingActionButton,
  LargeFloatingActionButton,
  Text,
} from "@expo/ui/jetpack-compose";
import { defaultMinSize, height, size, width } from "@expo/ui/jetpack-compose/modifiers";
import { View } from "react-native";
import { useAndroidControlSizing } from "./useAndroidControlSizing";
import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole";
import { SymbolView } from "./AppSymbol";

export function FloatingActionButton(props: FloatingActionButtonProps) {
  const { themeVariables: colors } = useAppearancePreferences();
  const typography = useScaledTextRole("footnote");
  const { scale, iconSize: standardIconSize, fabSize, largeFabSize } = useAndroidControlSizing();
  const buttonSize = props.variant === "large" ? largeFabSize : fabSize;
  const primary = props.tone === "primary";
  const containerColor = colors[primary ? "--color-primary" : "--color-secondary"];
  const contentColor =
    colors[primary ? "--color-primary-foreground" : "--color-secondary-foreground"];
  const Component =
    props.variant === "extended"
      ? ExtendedFloatingActionButton
      : props.variant === "large"
        ? LargeFloatingActionButton
        : ComposeFloatingActionButton;
  const iconSize = props.variant === "large" ? Math.round(36 * scale) : standardIconSize;
  return (
    <View
      accessible
      accessibilityRole="button"
      accessibilityLabel={props.label}
      accessibilityActions={[{ name: "activate" }]}
      onAccessibilityAction={props.onPress}
      className={props.className}
      style={props.style}
    >
      <View importantForAccessibility="no-hide-descendants">
        <NativeThemeHost matchContents ignoreSafeAreaKeyboardInsets>
          <Component
            modifiers={[
              defaultMinSize({ minWidth: buttonSize }),
              height(buttonSize),
              ...(props.variant === "extended" ? [] : [width(buttonSize)]),
            ]}
            containerColor={containerColor}
            onClick={props.onPress}
            expanded={props.expanded}
          >
            <Component.Icon>
              <Box modifiers={[size(iconSize, iconSize)]} />
            </Component.Icon>
            {props.variant === "extended" ? (
              <ExtendedFloatingActionButton.Text>
                <Text color={contentColor} style={{ ...typography, fontWeight: "500" }}>
                  {props.label}
                </Text>
              </ExtendedFloatingActionButton.Text>
            ) : null}
          </Component>
        </NativeThemeHost>
      </View>
      {/* The RN icon stays outside Compose so it cannot intercept native button taps. */}
      <View
        pointerEvents="none"
        className="absolute inset-y-0 justify-center"
        style={
          props.variant === "extended" ? { left: 16 } : { left: 0, right: 0, alignItems: "center" }
        }
      >
        <SymbolView
          name={props.icon}
          size={iconSize}
          tintColorClassName={primary ? "accent-primary-foreground" : "accent-secondary-foreground"}
        />
      </View>
    </View>
  );
}