UIPackage React Native
Search

Map Preview

Embeddable native map with opt-in gestures, markers, route lines, and an accessible fallback.

The browser shows the map fallback. To try the native map, open the catalog on iPhone or in Expo Go on Android.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add map-preview

Source files

Expand a file to read the source the installer copies into your app.

components/ui/map-preview/MapPreview.tsx
import * as React from 'react'
import { StyleSheet, View } from 'react-native'
import MapView from 'react-native-maps'
import { cn } from '@/lib/utils'
import { MapPreviewFallback } from './MapPreviewFallback'
import type { MapPreviewProps } from './MapPreview.types'

export const MapPreview = React.forwardRef<React.ComponentRef<typeof MapView>, MapPreviewProps>(
  (
    {
      className,
      style,
      testID,
      available = true,
      interactive = false,
      fallback,
      accessible,
      accessibilityLabel = 'Map',
      accessibilityRole,
      accessibilityHint,
      accessibilityElementsHidden,
      importantForAccessibility,
      scrollEnabled,
      zoomEnabled,
      rotateEnabled,
      pitchEnabled,
      showsMyLocationButton = false,
      showsUserLocation = false,
      ...props
    },
    ref,
  ) => {
    if (!available) return <MapPreviewFallback {...{ className, style, testID, fallback }} />

    return (
      <View
        style={style}
        accessible={interactive ? undefined : (accessible ?? true)}
        accessibilityLabel={interactive ? undefined : accessibilityLabel}
        accessibilityRole={interactive ? undefined : (accessibilityRole ?? 'image')}
        accessibilityHint={interactive ? undefined : accessibilityHint}
        className={cn('min-h-[240px] overflow-hidden rounded-lg border border-border bg-muted', className)}
      >
        <MapView
          {...props}
          ref={ref}
          testID={testID}
          style={StyleSheet.absoluteFill}
          pointerEvents={interactive ? 'auto' : 'none'}
          accessible={interactive ? accessible : false}
          accessibilityLabel={interactive ? accessibilityLabel : undefined}
          accessibilityRole={interactive ? accessibilityRole : undefined}
          accessibilityHint={interactive ? accessibilityHint : undefined}
          accessibilityElementsHidden={interactive ? accessibilityElementsHidden : true}
          importantForAccessibility={interactive ? importantForAccessibility : 'no-hide-descendants'}
          showsMyLocationButton={showsMyLocationButton}
          showsUserLocation={showsUserLocation}
          scrollEnabled={interactive && (scrollEnabled ?? true)}
          zoomEnabled={interactive && (zoomEnabled ?? true)}
          rotateEnabled={interactive && (rotateEnabled ?? true)}
          pitchEnabled={interactive && (pitchEnabled ?? true)}
        />
      </View>
    )
  },
)
MapPreview.displayName = 'MapPreview'

export { Marker as MapPreviewMarker, Polyline as MapPreviewPolyline } from 'react-native-maps'
components/ui/map-preview/MapPreview.web.tsx
import type { ComponentType } from 'react'
import type { MapMarkerProps, MapPolylineProps } from 'react-native-maps'
import { MapPreviewFallback } from './MapPreviewFallback'
import type { MapPreviewProps } from './MapPreview.types'

export function MapPreview({ className, style, testID, fallback }: MapPreviewProps) {
  return <MapPreviewFallback {...{ className, style, testID, fallback }} />
}

export const MapPreviewMarker: ComponentType<MapMarkerProps> = () => null
export const MapPreviewPolyline: ComponentType<MapPolylineProps> = () => null
components/ui/map-preview/MapPreview.types.ts
import type { ReactNode } from 'react'
import type { ViewProps } from 'react-native'
import type { MapViewProps } from 'react-native-maps'

export interface MapPreviewProps extends Omit<MapViewProps, 'style'> {
  className?: string
  style?: ViewProps['style']
  /** Render the fallback when the app's map provider is unavailable. */
  available?: boolean
  /** Opt into map gestures when users need to explore an embedded map. */
  interactive?: boolean
  /** Accessible alternative, such as a list of stops or pinned addresses. */
  fallback?: ReactNode
}

export type { LatLng, Region } from 'react-native-maps'
components/ui/map-preview/MapPreviewFallback.tsx
import { View } from 'react-native'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'
import type { MapPreviewProps } from './MapPreview.types'

export function MapPreviewFallback({
  className,
  style,
  testID,
  fallback,
}: Pick<MapPreviewProps, 'className' | 'style' | 'testID' | 'fallback'>) {
  return (
    <View
      testID={testID}
      style={style}
      className={cn('min-h-[240px] justify-center rounded-lg border border-border bg-muted p-4', className)}
    >
      {renderTextChildren(fallback ?? 'Map unavailable', 'text-muted-foreground')}
    </View>
  )
}
components/ui/map-preview/index.ts
export { MapPreview, MapPreviewMarker, MapPreviewPolyline } from './MapPreview'
export type { LatLng, MapPreviewProps, Region } from './MapPreview.types'

Dependencies

Registry items

npm packages

  • react-native-maps
Browse content and media