适用于 iOS 和 Android 的 React Native 模糊覆盖库
A native blur overlay for React Native: it blurs whatever is rendered behind it and lets you put your own content on top — full-screen, or just one rounded panel, which is the closest you can get to iOS's frosted-glass materials on Android.
The example app on the New Architecture. The first two are the same glass panel, same code, on iOS and Android; then a full-screen overlay on each.
UIVisualEffectView material on iOS gives you a matching glass panel on AndroidMainApplication edits3.0 is a maintenance release that rewrites both native sides and the JS API. See Migrating from 2.x.
| React Native | >= 0.80 (New Architecture and legacy both supported) |
| iOS | 15.1+ |
| Android | minSdk 24+ |
For older React Native versions use react-native-blur-overlay@2.
npm install react-native-blur-overlaycd ios && pod installThat's it — the library is autolinked. If you are upgrading from 2.x, remove the manual pod 'SajjadBlurOverlay', :path => ... line from your Podfile and the manual add(SajjadBlurOverlayPackage()) call from MainApplication.
…1. The openOverlay / closeOverlay helpers — handy when the overlay lives far away from the code that opens it:
import BlurOverlay, { closeOverlay, openOverlay } from 'react-native-blur-overlay';
closeOverlay()}>{/* ... */};
openOverlay();Pass an id when more than one overlay can be mounted at the same time, and use the same id on both sides:
;
openOverlay('settings');
closeOverlay('settings');2. A ref, if you would rather not use module-level functions:
const overlay = useRef(null);
overlay.current?.close()}>{/* ... */};
overlay.current?.open();3. The visible prop, for fully declarative control (also the way to keep the overlay up permanently):
const [visible, setVisible] = useState(false);
setVisible(false)} />;
// Always blurred:
When visible is set, the imperative API is ignored for that overlay.
| Prop | Type | Default | Platform | Description |
|---|---|---|---|---|
visible |
boolean |
— | both | Controls the overlay declaratively. When set, the imperative API is ignored. |
id |
string |
'default' |
both | Id used by openOverlay(id) / closeOverlay(id). |
radius |
number |
20 |
Android | Blur radius. Effective radius is radius / downsampling, capped at 25. |
downsampling |
number |
1 |
Android | How much the snapshot is scaled down before blurring. Higher is faster and coarser. |
brightness |
number |
0 |
Android | Brightness offset, -255..255. Negative darkens. |
blurStyle |
BlurStyle |
'light' |
iOS | Which UIBlurEffectStyle to use, see below. |
vibrant |
boolean |
false |
iOS | Renders the children inside a UIVibrancyEffect view. |
fadeDuration |
number |
500 |
both | Fade in/out duration in ms. 0 disables the animation. |
onPress |
() => void |
— | both | Called when the blurred backdrop is pressed. |
closeOnChildPress |
boolean |
false |
both | Also report presses that land on the children through onPress. |
backdropAccessibilityLabel |
string |
'Close overlay' |
both | Accessibility label of the pressable backdrop. |
onShow |
() => void |
— | both | Called once the overlay is fully faded in. |
onHide |
() => void |
— | both | Called once the overlay is fully faded out. |
style |
StyleProp |
fills the parent | both | Style of the blurred surface. |
children |
ReactNode |
— | both | Content rendered on top of the blur. |
BlurStyle is one of 'light', 'extraLight', 'dark', 'regular', 'prominent', 'systemUltraThinMaterial', 'systemThinMaterial', 'systemMaterial', 'systemThickMaterial', 'systemChromeMaterial'. The system* styles follow the device's light/dark appearance.
Deprecated aliases, still honoured: idBlur (→ id), customStyles (→ style), animationDuration (→ fadeDuration).
import BlurOverlay, {
openOverlay,
closeOverlay,
DEFAULT_ID,
type BlurOverlayProps,
type BlurOverlayInstance,
type BlurStyle,
} from 'react-native-blur-overlay';| Export | Description |
|---|---|
openOverlay(id?) |
Fades in the overlay mounted with that id. |
closeOverlay(id?) |
Fades it out. |
DEFAULT_ID |
The id used when none is given. |
ref.open() / ref.close() |
The same, per instance. |
iOS uses a UIVisualEffectView, so the blur is live: whatever moves behind the overlay stays blurred while it moves.
Android has no equivalent system view, so the library takes a snapshot of the screen behind the overlay when the overlay appears, blurs it off the main thread and draws it as the overlay's background. Two things follow from that:
borderRadius, borderWidth and backgroundColor on the overlay itself have no effect on Android — wrap the overlay in a rounded, overflow: 'hidden' parent instead.Blurring used to be done with RenderScript, which Android deprecated in Android 12 and no longer ships to new builds. 3.0 replaces it with a Stack Blur implementation that works on every supported API level.
A blur overlay does not have to cover the screen. Size it to a box and you get a frosted-glass surface — a sheet, a card, a tab bar — on both platforms:
// The parent's rounded corners are what shape the glass, on both platforms.
const styles = StyleSheet.create({
clip: {
position: 'absolute',
left: 20,
right: 20,
bottom: 40,
height: 190,
borderRadius: 28,
overflow: 'hidden',
},
panel: {
width: '100%',
height: '100%',
borderRadius: 28,
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'rgba(255,255,255,0.35)',
justifyContent: 'center',
padding: 22,
gap: 8,
},
});
Frosted glass
Same code on iOS and Android.
Two things to know:
borderRadius, borderWidth and
backgroundColor set on the overlay itself are not applied there — a
rounded, overflow: 'hidden' parent clips it on both platforms instead.Works on both platforms — iOS blurs whatever is behind the overlay's own frame, and Android crops its snapshot to the overlay's position on screen. Give the overlay an explicit position and size, or mount it inside the view you want blurred:
// Inside the view you want blurred — the overlay fills its parent:
// Or with an explicit box (the defaults pin all four edges, so release the
// two you replace):
This is the default: presses that land on the children are left to the children, and only presses on the backdrop call onPress. Note that a plain View does not capture touches in React Native — the library wraps your children in a touch-claiming view sized to them. Pass closeOnChildPress if you want every press to call onPress.
Blurring a full-screen snapshot costs the most on Android. Raise downsampling (2–4 is a good range) — the snapshot is scaled down before blurring, and the effective radius is adjusted so the result looks the same.
openOverlay() does nothing. The id has to match on both sides: an overlay rendered with id="x" only listens to openOverlay('x'). In development the library warns when a call finds no matching overlay. Overlays driven by the visible prop deliberately ignore these calls.
The Android blur shows a stale screen. 3.0 takes the snapshot when the overlay is mounted, so opening it right after a navigation transition can still catch the tail of the animation. Delay the openOverlay() call until the transition has settled.
The blur is transparent/black on Android. view.draw() cannot capture hardware surfaces — video players, camera previews, maps and other SurfaceView-based content come out blank. That is a platform limitation of snapshot-based blurring.
Podfile entry and the manual SajjadBlurOverlayPackage registration.idBlur → id, customStyles → style, animationDuration → fadeDuration. The old names still work.onPress no longer fires for presses on your children. Pass closeOnChildPress to get the old behaviour.radius is now capped at 25 after downsampling.index.tsx as the package entry point and imported Node's events module, which Metro cannot resolve; 3.0 ships a compiled build with type definitions.The repo ships a small app that exercises every prop — imperative and declarative opening, the iOS blur styles, the Android radius/downsampling, a partial-screen overlay and press handling:
npm install
npm run example:start # in one terminal
npm run example:android # or: npm run example:iosIt is an npm workspace of the library and Metro resolves the library to its
TypeScript sources, so editing src/ refreshes the app without a rebuild.
npm install # installs deps and builds the library
npm run typecheck
npm test
npm run check:android # compiles the Java against React Native's classes
npm run check:ios # type-checks the Obj-C(++) on both architectures (needs Xcode)The two native checks need neither an example app nor a CocoaPods/Gradle
install: they fetch React Native's own headers and classes for the version in
node_modules and run clang/javac against the Codegen output. CI runs all
of them.
The Codegen spec lives in src/SajjadBlurOverlayNativeComponent.ts; the native implementations are in android/src/main/java/com/bluroverly and ios.
Releases are published from CI with npm Trusted Publishing: bump the version, tag it vX.Y.Z and push the tag.
MIT © Sajjad Asadi
暂无开放 Issues,或尚未同步最近议题。