Skip to content

Commit 5a0865c

Browse files
authored
Edge-to-edge support (#103)
* feat: force bottom sheet edge to edge * fix: add onStart * fix: android < 30 screen height is wrong * chore: return if * chore: simplify condition * fix: typo * feat: add edgeToEdge prop
1 parent d73a397 commit 5a0865c

File tree

8 files changed

+81
-5
lines changed

8 files changed

+81
-5
lines changed

android/src/main/java/com/lodev09/truesheet/TrueSheetDialog.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
4242
var footerHeight = 0
4343
var maxSheetHeight: Int? = null
4444

45+
var edgeToEdge: Boolean = false
46+
set(value) {
47+
field = value
48+
maxScreenHeight = Utils.screenHeight(reactContext, value)
49+
}
50+
4551
var dismissible: Boolean = true
4652
set(value) {
4753
field = value
@@ -67,7 +73,26 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
6773
}
6874

6975
// Update the usable sheet height
70-
maxScreenHeight = Utils.screenHeight(reactContext)
76+
maxScreenHeight = Utils.screenHeight(reactContext, edgeToEdge)
77+
}
78+
79+
override fun getEdgeToEdgeEnabled(): Boolean {
80+
return edgeToEdge || super.getEdgeToEdgeEnabled()
81+
}
82+
83+
override fun onStart() {
84+
super.onStart()
85+
86+
if (edgeToEdge) {
87+
window?.apply {
88+
setFlags(
89+
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
90+
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
91+
)
92+
93+
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
94+
}
95+
}
7196
}
7297

7398
/**
@@ -226,7 +251,7 @@ class TrueSheetDialog(private val reactContext: ThemedReactContext, private val
226251
override fun onKeyboardStateChange(isVisible: Boolean, visibleHeight: Int?) {
227252
maxScreenHeight = when (isVisible) {
228253
true -> visibleHeight ?: 0
229-
else -> Utils.screenHeight(reactContext)
254+
else -> Utils.screenHeight(reactContext, edgeToEdge)
230255
}
231256

232257
positionFooter()

android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ class TrueSheetView(context: Context) :
240240
}
241241
}
242242

243+
fun setEdgeToEdge(edgeToEdge: Boolean) {
244+
sheetDialog.edgeToEdge = edgeToEdge
245+
}
246+
243247
fun setMaxHeight(height: Int) {
244248
if (sheetDialog.maxSheetHeight == height) return
245249

android/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class TrueSheetViewManager : ViewGroupManager<TrueSheetView>() {
3232
.put(SizeChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onSizeChange"))
3333
.build()
3434

35+
@ReactProp(name = "edgeToEdge")
36+
fun setEdgeToEdge(view: TrueSheetView, edgeToEdge: Boolean) {
37+
view.setEdgeToEdge(edgeToEdge)
38+
}
39+
3540
@ReactProp(name = "maxHeight")
3641
fun setMaxHeight(view: TrueSheetView, height: Double) {
3742
view.setMaxHeight(Utils.toPixel(height))

android/src/main/java/com/lodev09/truesheet/core/Utils.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Utils {
1818
).takeIf { it > 0 } ?: 0
1919

2020
@SuppressLint("InternalInsetResource", "DiscouragedApi")
21-
fun screenHeight(context: ReactContext): Int {
21+
fun screenHeight(context: ReactContext, edgeToEdge: Boolean): Int {
2222
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
2323
val displayMetrics = DisplayMetrics()
2424

@@ -45,7 +45,16 @@ object Utils {
4545
0
4646
}
4747

48-
return screenHeight - statusBarHeight - navigationBarHeight
48+
return if (edgeToEdge) {
49+
// getRealMetrics includes navigation bar height
50+
// windowManager.defaultDisplay.getMetrics doesn't
51+
when (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
52+
true -> screenHeight
53+
false -> screenHeight + navigationBarHeight
54+
}
55+
} else {
56+
screenHeight - statusBarHeight - navigationBarHeight
57+
}
4958
}
5059

5160
fun toDIP(value: Int): Float = PixelUtil.toDIPFromPixel(value.toFloat())

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
"publishConfig": {
5858
"registry": "https://registry.npmjs.org/"
5959
},
60+
"dependencies": {
61+
"react-native-is-edge-to-edge": "^1.1.6"
62+
},
6063
"devDependencies": {
6164
"@commitlint/config-conventional": "^19.0.3",
6265
"@evilmartians/lefthook": "^1.6.5",
@@ -139,7 +142,8 @@
139142
"prettier"
140143
],
141144
"globals": {
142-
"it": false
145+
"it": false,
146+
"__DEV__": true
143147
},
144148
"rules": {
145149
"@typescript-eslint/no-var-requires": 0,

src/TrueSheet.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type NativeSyntheticEvent,
1010
type LayoutChangeEvent,
1111
} from 'react-native'
12+
import { isEdgeToEdge, controlEdgeToEdgeValues } from 'react-native-is-edge-to-edge'
1213

1314
import type { TrueSheetProps, SizeInfo } from './TrueSheet.types'
1415
import { TrueSheetModule } from './TrueSheetModule'
@@ -22,6 +23,8 @@ const LINKING_ERROR =
2223
'- You rebuilt the app after installing the package\n' +
2324
'- You are not using Expo Go\n'
2425

26+
const EDGE_TO_EDGE = isEdgeToEdge()
27+
2528
interface TrueSheetNativeViewProps extends Omit<TrueSheetProps, 'onPresent' | 'onSizeChange'> {
2629
contentHeight?: number
2730
footerHeight?: number
@@ -194,6 +197,10 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
194197
)
195198
}
196199

200+
if (__DEV__) {
201+
controlEdgeToEdgeValues({ edgeToEdge: this.props.edgeToEdge })
202+
}
203+
197204
this.updateState()
198205
}
199206

@@ -209,6 +216,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
209216
grabber = true,
210217
dimmed = true,
211218
initialIndexAnimated = true,
219+
edgeToEdge = false,
212220
keyboardMode = 'resize',
213221
initialIndex,
214222
dimmedIndex,
@@ -236,6 +244,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
236244
grabber={grabber}
237245
dimmed={dimmed}
238246
dimmedIndex={dimmedIndex}
247+
edgeToEdge={EDGE_TO_EDGE || edgeToEdge}
239248
initialIndex={initialIndex}
240249
initialIndexAnimated={initialIndexAnimated}
241250
keyboardMode={keyboardMode}

src/TrueSheet.types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ export interface TrueSheetProps extends ViewProps {
230230
*/
231231
keyboardMode?: 'resize' | 'pan'
232232

233+
/**
234+
* Determines how the software keyboard will impact the layout of the sheet.
235+
* Set to `pan` if you're working with `FlatList` with a `TextInput`.
236+
*
237+
* @platform android
238+
* @default `true` is `react-native-edge-to-edge` is installed, `false` otherwise
239+
*/
240+
edgeToEdge?: boolean
241+
233242
/**
234243
* This is called when the sheet is ready to present.
235244
*/

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,7 @@ __metadata:
36723672
react: "npm:^18.2.0"
36733673
react-native: "npm:^0.74.1"
36743674
react-native-builder-bob: "npm:^0.23.2"
3675+
react-native-is-edge-to-edge: "npm:^1.1.6"
36753676
release-it: "npm:^17.1.1"
36763677
typescript: "npm:~5.3.3"
36773678
peerDependencies:
@@ -18329,6 +18330,16 @@ __metadata:
1832918330
languageName: node
1833018331
linkType: hard
1833118332

18333+
"react-native-is-edge-to-edge@npm:^1.1.6":
18334+
version: 1.1.6
18335+
resolution: "react-native-is-edge-to-edge@npm:1.1.6"
18336+
peerDependencies:
18337+
react: ">=18.2.0"
18338+
react-native: ">=0.73.0"
18339+
checksum: 10c0/5690e521e8310d21643634a8d0dacd524e19b76695f347b26f649fcac156a7a901fd6daef7f78482381a41e8445f4552f40ade13790fdf112fab15b0f54dbabd
18340+
languageName: node
18341+
linkType: hard
18342+
1833218343
"react-native-maps@npm:1.14.0":
1833318344
version: 1.14.0
1833418345
resolution: "react-native-maps@npm:1.14.0"

0 commit comments

Comments
 (0)