Skip to content

Commit

Permalink
feat(ios): add dimmedIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed Apr 25, 2024
1 parent 4429135 commit 678bb84
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
18 changes: 18 additions & 0 deletions docs/docs/reference/01-props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ If set to `false`, the sheet will prevent interactive dismissal via dragging or
| - | - | - | - |
| `boolean` | `true` | | |

### `dimmed`

Specify whether the sheet background is dimmed. Set to `false` to allow interaction with the background components.

| Type | Default | 🍎 | 🤖 |
| - | - | - | - |
| `boolean` | `true` | | |

### `dimmedIndex`

The size index that the sheet should start to dim on IOS. When value is `> 0`, `dimmed` is ignored.

Consider using `dimmed` for consistency across platforms.

| Type | Default | 🍎 | 🤖 |
| - | - | - | - |
| `number` | `0` | | |

### `grabber`

Shows a grabber (or handle). Native on IOS and styled `View` on Android.
Expand Down
6 changes: 4 additions & 2 deletions example/src/sheets/InlineSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ export const InlineSheet = forwardRef((props: InlineSheetProps, ref: Ref<InlineS
<TrueSheet
ref={sheetRef}
dimmed={false}
sizes={['auto', 'large']}
dimmedIndex={1}
sizes={['auto', '75%', 'large']}
blurTint="dark"
backgroundColor={DARK}
onDismiss={() => setIsPresented(false)}
contentContainerStyle={$content}
{...props}
>
<DemoContent color={BLUE} />
<DemoContent color={BLUE} />
<Button text="Dismiss" onPress={dismiss} />
</TrueSheet>
Expand All @@ -69,7 +71,7 @@ const $content: ViewStyle = {
}

const $backgroundContent: ViewStyle = {
padding: SPACING,
padding: 24,
paddingTop: SPACING * 6,
backgroundColor: BLUE,
}
Expand Down
11 changes: 11 additions & 0 deletions ios/TrueSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
}
}

@objc
func setDimmedIndex(_ index: NSNumber) {
viewController.dimmedIndex = index as? Int

if #available(iOS 15.0, *) {
withPresentedSheet { sheet in
viewController.setDimmed(for: sheet)
}
}
}

@objc
func setScrollableHandle(_ tag: NSNumber?) {
let view = bridge.uiManager.view(forReactTag: tag) as? RCTScrollView
Expand Down
15 changes: 11 additions & 4 deletions ios/TrueSheetViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class TrueSheetViewController: UIViewController, UISheetPresentationControllerDe
var cornerRadius: CGFloat?
var grabber = true
var dimmed = true
var dimmedIndex: Int? = 0

// MARK: - Setup

Expand Down Expand Up @@ -127,15 +128,21 @@ class TrueSheetViewController: UIViewController, UISheetPresentationControllerDe
}
}

/// Setup dimmed sheet.
/// `dimmedIndex` will further customize the dimming behavior.
@available(iOS 15.0, *)
func setDimmed(for sheet: UISheetPresentationController) {
if dimmed {
if dimmed, dimmedIndex == 0 {
sheet.largestUndimmedDetentIdentifier = nil
} else {
sheet.largestUndimmedDetentIdentifier = .large
if #available(iOS 16.0, *),
let lastIdentifier = sheet.detents.last?.identifier {
sheet.largestUndimmedDetentIdentifier = lastIdentifier

if #available(iOS 16.0, *) {
if let dimmedIndex, sheet.detents.indices.contains(dimmedIndex - 1) {
sheet.largestUndimmedDetentIdentifier = sheet.detents[dimmedIndex - 1].identifier
} else if let lastIdentifier = sheet.detents.last?.identifier {
sheet.largestUndimmedDetentIdentifier = lastIdentifier
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ios/TrueSheetViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @interface RCT_EXTERN_REMAP_MODULE (TrueSheetView, TrueSheetViewManager, RCTView
RCT_EXPORT_VIEW_PROPERTY(grabber, BOOL)
RCT_EXPORT_VIEW_PROPERTY(dismissible, BOOL)
RCT_EXPORT_VIEW_PROPERTY(dimmed, BOOL)
RCT_EXPORT_VIEW_PROPERTY(dimmedIndex, NSNumber)

// Internal properties
RCT_EXPORT_VIEW_PROPERTY(contentHeight, NSNumber)
Expand Down
2 changes: 2 additions & 0 deletions src/TrueSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
dismissible = true,
grabber = true,
dimmed = true,
dimmedIndex,
grabberProps,
blurTint,
cornerRadius,
Expand All @@ -226,6 +227,7 @@ export class TrueSheet extends PureComponent<TrueSheetProps, TrueSheetState> {
footerHeight={this.state.footerHeight}
grabber={grabber}
dimmed={dimmed}
dimmedIndex={dimmedIndex}
dismissible={dismissible}
maxHeight={maxHeight}
onPresent={this.onPresent}
Expand Down
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,20 @@ export interface TrueSheetProps extends ViewProps {
* Specify whether the sheet background is dimmed.
* Set to `false` to allow interaction with the background components.
*
* @platform android
* @platform ios 15+
* @default true
*/
dimmed?: boolean

/**
* The size index that the sheet should start to dim in IOS.
* The size index that the sheet should start to dim on IOS.
* When value is `> 0`, `dimmed` is ignored.
*
* @platform ios
* Consider using `dimmed` for consistency across platforms.
*
* @platform ios 16+
* @default 0
*/
dimmedIndex?: number

Expand Down

0 comments on commit 678bb84

Please sign in to comment.