Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Platform } from 'nitro-ui-core';
import { PlatformScreen } from './PlatformScreen';
import { ShadowsScreen } from './ShadowsScreen';
import { SplitViewScreen } from './SplitViewScreen';
import { BlurScreen } from './BlurScreen';
import homeIcon from './assets/icons/home.svg';
import sidebarIcon from './assets/icons/sidebar.svg';
import textIcon from './assets/icons/text.svg';
Expand All @@ -45,6 +46,7 @@ type Routes = {
Colors: undefined
Appearance: undefined
Shadows: undefined
Blur: undefined
MaskedView: undefined
Platform: undefined
Modal: undefined
Expand Down Expand Up @@ -167,6 +169,7 @@ function HomeScreen() {
<Item text="Text Input" onTap={() => navigation.push('TextInput')} />
<Item text="Linear gradients" onTap={() => navigation.push('LinearGradient')} />
{Application.supportsShadows && <Item text="Shadows" onTap={() => navigation.push('Shadows')} />}
<Item text="Blur" onTap={() => navigation.push('Blur')} />
<Item text="Colors" onTap={() => navigation.push('Colors')} />
<Item text="Appearance" onTap={() => navigation.push('Appearance')} />
<Item text="Alpha masks" onTap={() => navigation.push('MaskedView')} />
Expand Down Expand Up @@ -220,6 +223,7 @@ function ExamplesTab() {
<Stack.Screen name="TextInput" component={TextInputScreen} options={{ title: 'Text Input' }} />
<Stack.Screen name="LinearGradient" component={LinearGradientScreen} options={{ title: 'Linear gradients' }} />
<Stack.Screen name="Shadows" component={ShadowsScreen} />
<Stack.Screen name="Blur" component={BlurScreen} />
<Stack.Screen name="Colors" component={ColorsScreen} options={{ headerTintColor: '#e5484d' }} />
<Stack.Screen name="Appearance" component={AppearanceScreen} />
<Stack.Screen name="MaskedView" component={MaskedViewScreen} options={{ title: 'Alpha masks' }} />
Expand Down
127 changes: 127 additions & 0 deletions apps/example/src/BlurScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { useMemo, useState } from 'react'
import { Platform } from 'nitro-ui-core'
import {
Blur, Image, ScrollView, ScrollViewContent, TapGestureRecognizer, Text, View, useSafeAreaInsets,
type BlurGradient,
} from 'nitro-ui-renderer'
import meadow from './assets/images/blur-meadow.jpg'

const android = Platform.OS === 'android'
const colors = ['#2455d9', '#d12563', '#6636bd', '#047c85', '#ba5424']
const barBlur: BlurGradient = { axis: 'vertical', startRadius: 0, endRadius: 28 }
const photoBlur: BlurGradient = { axis: 'vertical', startRadius: 0, endRadius: 24 }

export function BlurScreen() {
const [enabled, setEnabled] = useState(true)
const [showPhoto, setShowPhoto] = useState(false)
const insets = useSafeAreaInsets()
const toggleBlur = useMemo(() => {
const recognizer = new TapGestureRecognizer()
recognizer.onTap = () => setEnabled((current) => !current)
return recognizer
}, [])
const toggleExample = useMemo(() => {
const recognizer = new TapGestureRecognizer()
recognizer.onTap = () => setShowPhoto((current) => !current)
return recognizer
}, [])

return (
<View
layout={{
flex: 1,
paddingHorizontal: 16,
paddingTop: 12 + (android ? 0 : insets.top),
paddingBottom: android ? 0 : insets.bottom,
gap: 10,
}}
style={{ background: '#f4f6fe' }}
>
<View layout={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', gap: 8 }}>
<Text layout={{ flexShrink: 1 }} fontSize={20} fontWeight="bold" color="#142450">
{showPhoto ? 'Progressive blur' : 'Scroll behind the tab bar'}
</Text>
<Text
gestureRecognizers={[toggleBlur]}
layout={{ paddingVertical: 9, paddingHorizontal: 12, overflow: 'hidden' }}
style={{ background: enabled ? '#2250cc' : '#e5eaff', cornerRadius: 9 }}
color={enabled ? '#ffffff' : '#193384'}
fontSize={13}
fontWeight="semibold"
>
{enabled ? 'Blur on' : 'Blur off'}
</Text>
</View>
{android && (
<Text
gestureRecognizers={[toggleExample]}
layout={{ alignSelf: 'flex-start', paddingVertical: 7 }}
color="#2250cc"
fontSize={14}
fontWeight="semibold"
>
{showPhoto ? 'Show scroll example' : 'Show progressive blur'}
</Text>
)}
{showPhoto ? <ProgressivePhoto enabled={enabled} /> : <ScrollExample enabled={enabled} />}
</View>
)
}

function ScrollExample({ enabled }: { enabled: boolean }) {
return (
<Blur.Root layout={{ flex: 1, overflow: 'hidden' }} style={{ background: '#e5eaff', cornerRadius: 16 }}>
<Blur.Background>
<ScrollView layout={{ flex: 1 }} showsVerticalScrollIndicator={false}>
<ScrollViewContent layout={{ gap: 7, padding: 8, paddingBottom: 92 }}>
{Array.from({ length: 32 }, (_, index) => (
<View
key={index}
layout={{ height: 54, flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 14, overflow: 'hidden' }}
style={{ background: colors[index % colors.length], cornerRadius: 10 }}
>
<Text color="#ffffff" fontWeight="bold" fontSize={20}>{String(index + 1).padStart(2, '0')}</Text>
<Text color="#ffffff" fontSize={15}>Moving content</Text>
</View>
))}
</ScrollViewContent>
</ScrollView>
</Blur.Background>
<Blur.Foreground
isUserInteractionEnabled={false}
material={enabled ? 'systemChromeMaterialDark' : 'none'}
android={{ radius: enabled ? barBlur : 0 }}
layout={{
position: 'absolute', left: 8, right: 8, bottom: 8, height: 62,
flexDirection: 'row', alignItems: 'center', justifyContent: 'space-around', overflow: 'hidden',
}}
style={{ background: '#121d4870', cornerRadius: 13 }}
>
<Text color="#ffffff" fontWeight="semibold" fontSize={14}>FEED</Text>
<Text color="#ffffff" fontWeight="semibold" fontSize={14}>SAVED</Text>
<Text color="#ffffff" fontWeight="semibold" fontSize={14}>PROFILE</Text>
</Blur.Foreground>
</Blur.Root>
)
}

function ProgressivePhoto({ enabled }: { enabled: boolean }) {
return (
<>
<Blur.Root
layout={{ width: '100%', aspectRatio: 1.15, overflow: 'hidden' }}
style={{ cornerRadius: 16 }}
>
<Blur.Background>
<Image image={meadow} resizeMode="cover" layout={{ flex: 1 }} />
</Blur.Background>
<Blur.Foreground
isUserInteractionEnabled={false}
android={{ radius: enabled ? photoBlur : 0 }}
layout={{ position: 'absolute', left: 0, top: 0, right: 0, bottom: 0 }}
/>
</Blur.Root>
<Text color="#25356c" fontSize={14}>0 dp at the top, 24 dp at the bottom</Text>
</>
)
}
Binary file added apps/example/src/assets/images/blur-meadow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions docs/components/figures/component-example.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,57 @@
.notes li:nth-child(3) .notebook { background-color: #9c5a1c; }
.notes li:nth-child(4) .notebook { background-color: #7a685e; }
.notes li:nth-child(5) .notebook { background-color: #b8410c; }

.blurRoot {
display: flex;
flex: 1;
flex-direction: column;
min-height: 0;
gap: 6px;
padding: 6px;
border: 1px dashed var(--accent);
border-radius: 12px;
}
.blurRoot code { font-size: 10px; font-weight: 650; }
.blurRoot > code { color: var(--accent); }
.blurLayers { position: relative; flex: 1; min-height: 0; overflow: hidden; border-radius: 8px; }
.blurBackground {
height: 100%;
overflow-y: auto;
overscroll-behavior: contain;
padding: 5px 5px 56px;
background: var(--tint);
scrollbar-width: thin;
}
.blurBackground:focus-visible { outline-offset: -2px; }
.blurBackground > code {
position: sticky;
z-index: 1;
top: 0;
display: block;
width: fit-content;
padding: 2px 5px;
border-radius: 4px;
background: var(--screen);
color: var(--accent);
}
.blurBackground ol { display: grid; gap: 4px; margin: 4px 0 0; padding: 0; list-style: none; }
.blurBackground li {
padding: 9px 5px;
border: 1px solid var(--edge);
border-radius: 6px;
background: color-mix(in srgb, var(--accent) 23%, var(--screen));
}
.blurBackground li:nth-child(even) { background: var(--screen); }
.blurForeground {
position: absolute;
inset: auto 5px 5px;
display: grid;
padding: 4px 7px;
border: 1px solid var(--accent);
border-radius: 7px;
background: color-mix(in srgb, var(--screen) 68%, transparent);
pointer-events: none;
}
.blurForeground[data-blur='true'] { backdrop-filter: blur(7px); }
.blurForeground code { color: var(--accent); }
46 changes: 46 additions & 0 deletions docs/components/figures/renderer-examples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,52 @@ export function ScrollViewExample() {
)
}

export function BlurExample() {
const [effect, setEffect] = useState<'Blur on' | 'Blur off'>('Blur on')
const enabled = effect === 'Blur on'
const code = `<Blur.Root layout={{ height: 240 }}>
<Blur.Background>
<ScrollView>
<ScrollViewContent>{rows}</ScrollViewContent>
</ScrollView>
</Blur.Background>
<Blur.Foreground
layout={barLayout}
material="${enabled ? 'systemThinMaterial' : 'none'}"
android={{ radius: ${enabled ? 16 : 0} }}
>
<Text>Fixed controls</Text>
</Blur.Foreground>
</Blur.Root>`

return (
<ComponentExample
name="Blur"
title="Blur layers"
caption="Scroll the background and toggle Blur. The foreground stays fixed and its label stays sharp."
code={code}
highlight="<Blur.Foreground"
>
<Choices label="Backdrop blur" options={['Blur on', 'Blur off']} value={effect} onChange={setEffect} />
<div className={styles.blurRoot}>
<code>Blur.Root</code>
<div className={styles.blurLayers}>
<div className={styles.blurBackground} role="region" aria-label="Blur.Background, scrollable content" tabIndex={0}>
<code>Blur.Background</code>
<ol>
{stops.map(stop => <li key={stop}>{stop}</li>)}
</ol>
</div>
<div className={styles.blurForeground} data-blur={enabled}>
<code>Blur.Foreground</code>
<strong>Fixed controls</strong>
</div>
</div>
</div>
</ComponentExample>
)
}

export function ScrollViewContentExample() {
const [spacing, setSpacing] = useState<'Compact' | 'Inset'>('Compact')
const padding = spacing === 'Compact' ? 0 : 20
Expand Down
65 changes: 65 additions & 0 deletions docs/content/docs/components/blur.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: Blur
description: Blur live content behind fixed controls while keeping the controls sharp.
---

import { BlurExample } from '@/components/figures/renderer-examples'

<BlurExample />

Use the three components together to place a blur behind a title, toolbar, or tab bar.

| Component | What it contains |
| --- | --- |
| `Blur.Root` | The dashed boundary in the preview. It groups one background and its foreground layers and supplies the source automatically. |
| `Blur.Background` | The screen content to sample, such as a scrolling list or an image. It fills the root by default. |
| `Blur.Foreground` | The region where blur is visible and the children drawn above it. Those children stay sharp. |

Place the background first and each foreground directly after it as siblings in the same root. Give the root a bounded height and position a foreground over the area you want to blur. A foreground sized to the bottom of the root makes a fixed toolbar while the list behind it scrolls.

## Blur behind a fixed toolbar

```tsx
import { Blur, ScrollView, ScrollViewContent, Text } from 'nitro-ui-renderer'

export function BlurredToolbar({ items }: { items: readonly string[] }) {
return (
<Blur.Root layout={{ height: 320, overflow: 'hidden' }}>
<Blur.Background style={{ background: '#e5eaff' }}>
<ScrollView>
<ScrollViewContent layout={{ gap: 12, padding: 16, paddingBottom: 76 }}>
{items.map(item => <Text key={item}>{item}</Text>)}
</ScrollViewContent>
</ScrollView>
</Blur.Background>
<Blur.Foreground
layout={{ position: 'absolute', left: 0, right: 0, bottom: 0, height: 60, padding: 16 }}
material="systemThinMaterial"
android={{ radius: 16 }}
isUserInteractionEnabled={false}
>
<Text>Fixed toolbar</Text>
</Blur.Foreground>
</Blur.Root>
)
}
```

Bottom padding keeps the final row reachable above the toolbar. Set `isUserInteractionEnabled={false}` on a decorative foreground to let touches reach the content beneath it. For an interactive toolbar, keep touch interaction enabled.

## Choose a material or radius

On iOS, set `material` to a UIKit material such as `systemThinMaterial` or `systemChromeMaterialDark`. The default is `systemMaterial`. Use `material="none"` to turn the effect off.

On Android, set `android.radius` to a number in dp for uniform blur, or give it an axis and two radii for progressive blur. The vertical axis runs from top to bottom and the horizontal axis from left to right. In this example the top starts at zero and the blur grows toward the bottom.

```tsx
<Blur.Foreground
layout={{ position: 'absolute', left: 0, right: 0, bottom: 0, height: 100 }}
android={{ radius: { axis: 'vertical', startRadius: 0, endRadius: 24 } }}
/>
```

Use a radius of zero or omit `android` to turn off Android blur. Uniform blur uses a GPU effect from API 31 and progressive blur from API 33. Android API 26 and later use an asynchronous CPU fallback where the GPU effect is unavailable. The fallback can trail moving content on older devices. Separately composited views such as `SurfaceView` fall outside the captured background.

For rounded edges, set `style.cornerRadius` and `layout.overflow: 'hidden'` on the foreground. Use a translucent `style.background` to tint the result. [ScrollView](/docs/components/scroll-view) · [Layout properties](/docs/layout#layout-properties)
1 change: 1 addition & 0 deletions docs/content/docs/components/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"image",
"(scroll-view)",
"list",
"blur",
"(masked-view)",
"view-controller-context"
]
Expand Down
47 changes: 47 additions & 0 deletions packages/nitro-ui-renderer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,53 @@ Offsets, blur and spread use logical units. Box shadows accept a nonnegative `bl

On Android, both styles use `RenderEffect` and require Android 12 (API 31) or later and hardware-accelerated drawing. Check `Application.supportsShadows` before applying them in an app that runs on older Android versions. Setting a nonempty shadow list on an unsupported version throws. On iOS, both styles use Core Animation. Shadows leave layout and hit targets unchanged.

## Backdrop blur

`Blur.Foreground` blurs the backdrop while keeping its children sharp. On iOS,
`material` selects a UIKit material such as `systemThinMaterial`,
`systemMaterial`, or `systemThickMaterialDark`. Use `material="none"` to
disable blur. Set the alpha on `style.background` to tint the result.

Place one `Blur.Background` followed by one or more `Blur.Foreground`
layers as siblings inside a `Blur.Root`. The root supplies the source
automatically on Android. A number gives a uniform radius. Use
`{ axis, startRadius, endRadius }` to vary the radius from top to bottom or
left to right. Specify radii in dp. They are capped at 150 physical pixels.

```tsx
import { Blur, Text } from 'nitro-ui-renderer'

export function BlurredLabel() {
return (
<Blur.Root layout={{ height: 160 }}>
<Blur.Background style={{ background: '#3569de' }}>
<Text>Content behind the blur</Text>
</Blur.Background>
<Blur.Foreground
layout={{ position: 'absolute', left: 0, right: 0, bottom: 0, height: 72 }}
material="systemThinMaterial"
android={{ radius: { axis: 'vertical', startRadius: 0, endRadius: 24 } }}
style={{ background: '#ffffff40' }}
>
<Text>Sharp label</Text>
</Blur.Foreground>
</Blur.Root>
)
}
```

Android uses a GPU effect for uniform blur from API 31 and progressive blur from
API 33. Earlier versions downsample the target and filter changing frames on a
worker, with captures capped at about 30 per second. On Android the blur
samples the drawing of `Blur.Background`. Separately composited surfaces such as
`SurfaceView` are outside that drawing. `style.background` provides a fallback fill
while the target is unavailable. For rounded corners, combine
`style.cornerRadius` with `layout.overflow: 'hidden'`.

Open **Blur** in the example app to see a list scroll under a fixed tab bar. On
Android, open **Show progressive blur** to compare a photograph with and without
a vertical blur gradient.

## View transforms

Scale and translation are flat style props:
Expand Down
Loading
Loading