/** * React Native's CSS parsers, for style values written as CSS strings. * * **Details** * * These helpers convert CSS strings, or React Native's object forms with * plain colours, into `Native` style values. Only helpers you use are bundled. * They target React Native 0.88.1's default `enableNativeCSSParsing: false`; * enabling the flag changes how native reads filter drop shadows and gradients. * `boxShadow`, `filter` and `backgroundImage` give `[]` for an invalid * value, including a colour React Native can't parse such as `oklch()`, as a * browser drops an invalid declaration. * * **Example** (Converting CSS values) * * ```ts * import { Css } from "@effect/ui-native/kit" * import type { Native } from "@effect/ui-native" * * const card: Native.ViewStyle = { * transform: Css.transform("rotate(45deg) scale(1.5)"), * boxShadow: Css.boxShadow("0 2px 8px rgba(0, 0, 0, 0.25)"), * backgroundImage: Css.backgroundImage("linear-gradient(#f00, #00f)") * } * ``` * * @stability experimental * @since 4.2.0 */ import * as Option from "effect/Option" import type * as Native from "../Native.ts" // One module per parser, so an unused helper leaves no import of React // Native behind in the pre-bundle. import processBackgroundImage from "./internal/processBackgroundImage.ts" import processBackgroundPosition from "./internal/processBackgroundPosition.ts" import processBackgroundRepeat from "./internal/processBackgroundRepeat.ts" import processBackgroundSize from "./internal/processBackgroundSize.ts" import processBoxShadow from "./internal/processBoxShadow.ts" import processColor from "./internal/processColor.ts" import processFilter from "./internal/processFilter.ts" import processTransform from "./internal/processTransform.ts" import processTransformOrigin from "./internal/processTransformOrigin.ts" /** * A length as CSS writes it, such as `"4px"`, or a number of * density-independent pixels. * * @stability experimental * @category models * @since 4.2.0 */ export type CssLength = number | string /** * A shadow in React Native's object form for {@link boxShadow}. * * @stability experimental * @category models * @since 4.2.0 */ export interface BoxShadowInput { readonly offsetX: CssLength readonly offsetY: CssLength readonly blurRadius?: CssLength | undefined readonly spreadDistance?: CssLength | undefined readonly inset?: boolean | undefined readonly color?: Native.Color | undefined } /** * A filter function in React Native's object form for {@link filter}. * Amounts may be numbers or CSS strings such as `"50%"` or `"90deg"`. * * @stability experimental * @category models * @since 4.2.0 */ export type FilterInput = | { readonly blur: CssLength } | { readonly brightness: CssLength } | { readonly contrast: CssLength } | { readonly grayscale: CssLength } | { readonly hueRotate: CssLength } | { readonly invert: CssLength } | { readonly opacity: CssLength } | { readonly saturate: CssLength } | { readonly sepia: CssLength } | { readonly dropShadow: string | { readonly offsetX: CssLength readonly offsetY: CssLength readonly standardDeviation?: CssLength | undefined readonly color?: Native.Color | undefined } } /** * A gradient in React Native's object form for {@link backgroundImage}. * * @stability experimental * @category models * @since 4.2.0 */ export type GradientInput = | { readonly type: "linear-gradient" /** An angle such as `"45deg"`, or a direction such as `"to right"`. */ readonly direction?: string | undefined readonly colorStops: ReadonlyArray } | { readonly type: "radial-gradient" readonly shape: "circle" | "ellipse" readonly size: | "closest-corner" | "closest-side" | "farthest-corner" | "farthest-side" | { readonly x: CssLength; readonly y: CssLength } readonly position: Native.Position readonly colorStops: ReadonlyArray } /** * A colour stop of a {@link GradientInput}, with up to two positions such as * `"20%"`. * * @stability experimental * @category models * @since 4.2.0 */ export interface ColorStopInput { readonly color: Native.Color readonly positions?: ReadonlyArray | undefined } /** * Converts a colour for a field that takes a `Native.ProcessedColor`, such * as the `color` of a `Native.BoxShadow`. Returns `None` for a colour * React Native can't process, such as `oklch()`. * * @stability experimental * @category constructors * @since 4.2.0 */ export const color = ( color: Native.Color ): Option.Option => Option.fromNullishOr(processColor(color)) /** * Parses a CSS `transform`, such as `"translateX(10px) rotate(45deg)"`. * * @stability experimental * @category constructors * @since 4.2.0 */ export const transform: ( transform: string ) => ReadonlyArray = processTransform /** * Parses a CSS `transform-origin`, such as `"top left"`. * * @stability experimental * @category constructors * @since 4.2.0 */ export const transformOrigin: ( origin: string ) => NonNullable = processTransformOrigin /** * Parses a CSS `box-shadow`, or converts React Native's object form. * * @stability experimental * @category constructors * @since 4.2.0 */ export const boxShadow: ( shadow: string | ReadonlyArray ) => ReadonlyArray = processBoxShadow /** * Parses a CSS `filter`, such as `"blur(4px) drop-shadow(0 2px 4px #000)"`, * or converts React Native's object form. * * @stability experimental * @category constructors * @since 4.2.0 */ export const filter: ( filter: string | ReadonlyArray ) => ReadonlyArray = processFilter /** * Parses CSS `linear-gradient(...)` and `radial-gradient(...)` images, or * converts React Native's object form. * * @stability experimental * @category constructors * @since 4.2.0 */ export const backgroundImage: ( image: string | ReadonlyArray ) => ReadonlyArray = processBackgroundImage /** * Parses a CSS `background-size`, such as `"50% auto"`. * * @stability experimental * @category constructors * @since 4.2.0 */ export const backgroundSize: ( size: string ) => NonNullable = processBackgroundSize /** * Parses a CSS `background-position`, such as `"center top"`. * * @stability experimental * @category constructors * @since 4.2.0 */ export const backgroundPosition: ( position: string ) => ReadonlyArray = processBackgroundPosition /** * Parses a CSS `background-repeat`, such as `"repeat-x"`. * * @stability experimental * @category constructors * @since 4.2.0 */ export const backgroundRepeat: ( repeat: string ) => NonNullable = processBackgroundRepeat