Skip to content

Commit

Permalink
⭐️ Impl: Sheet-Related Props
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-04-20-23-58-24` - Impl. sheets + detents.
* `TODO:2023-04-21-23-24-47` - Impl. prop `sheetLargestUndimmedDetentIdentifier`.
* `TODO:2023-04-21-23-26-13` - Impl. prop `sheetPreferredCornerRadius`.
* `TODO:2023-04-21-23-24-03` - Impl. prop `sheetSelectedDetentIdentifier`.
* `TODO:2023-04-21-23-24-39` - Impl. prop `sheetShouldAnimateChanges`.
  • Loading branch information
dominicstop committed Apr 21, 2023
1 parent dbf0e79 commit d7f2520
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import Foundation

@available(iOS 15.0, *)
extension UISheetPresentationController.Detent {
static func fromString(_ string: String) -> UISheetPresentationController.Detent? {

static func fromString(
_ string: String
) -> UISheetPresentationController.Detent? {

switch string {
case "medium": return .medium();
case "large" : return .large();
Expand All @@ -19,3 +23,24 @@ extension UISheetPresentationController.Detent {
};
};
};

@available(iOS 15.0, *)
extension UISheetPresentationController.Detent.Identifier {
init?(fromSystemIdentifierString string: String) {
switch string {
case "medium": self = .medium;
case "large" : self = .large;

default: return nil;
};
};

init(fromString string: String) {
if let systemIdentifier = Self.init(fromSystemIdentifierString: string) {
self = systemIdentifier;

} else {
self.init(string);
};
};
};
106 changes: 99 additions & 7 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ public class RNIModalView:
let sheetController = self.sheetPresentationController
else { return };

sheetController.prefersEdgeAttachedInCompactHeight = newValue;
self.sheetAnimateChangesIfNeeded {
sheetController.prefersEdgeAttachedInCompactHeight = newValue;
};
}
};

Expand All @@ -209,7 +211,50 @@ public class RNIModalView:
let sheetController = self.sheetPresentationController
else { return };

sheetController.prefersGrabberVisible = newValue;
self.sheetAnimateChangesIfNeeded {
sheetController.prefersGrabberVisible = newValue;
};
}
};

@objc var sheetShouldAnimateChanges: Bool = true;

@objc var sheetLargestUndimmedDetentIdentifier: String? {
willSet {
guard #available(iOS 15.0, *),
let sheetController = self.sheetPresentationController
else { return };

self.sheetAnimateChangesIfNeeded {
sheetController.largestUndimmedDetentIdentifier =
self.synthesizedSheetLargestUndimmedDetentIdentifier;
};
}
};

@objc var sheetPreferredCornerRadius: NSNumber? {
willSet {
guard #available(iOS 15.0, *),
let sheetController = self.sheetPresentationController,
let cornerRadius = newValue?.doubleValue
else { return };

self.sheetAnimateChangesIfNeeded {
sheetController.preferredCornerRadius = cornerRadius;
};
}
};

@objc var sheetSelectedDetentIdentifier: String? {
willSet {
guard #available(iOS 15.0, *),
let sheetController = self.sheetPresentationController
else { return };

self.sheetAnimateChangesIfNeeded {
sheetController.selectedDetentIdentifier =
self.synthesizedSheetSelectedDetentIdentifier;
};
}
};

Expand Down Expand Up @@ -315,14 +360,39 @@ public class RNIModalView:

} else if #available(iOS 16.0, *),
let dict = $0 as? Dictionary<String, Any> {
let customDetent = RNIModalCustomSheetDetent(forDict: dict)

let customDetent = RNIModalCustomSheetDetent(forDict: dict);
return customDetent?.synthesizedDetent;
};

return nil;
};
};

@available(iOS 15.0, *)
public var synthesizedSheetLargestUndimmedDetentIdentifier:
UISheetPresentationController.Detent.Identifier? {

guard let identifierString = self.sheetLargestUndimmedDetentIdentifier
else { return nil };

return UISheetPresentationController.Detent.Identifier(
fromString: identifierString
);
};

@available(iOS 15.0, *)
public var synthesizedSheetSelectedDetentIdentifier:
UISheetPresentationController.Detent.Identifier? {

guard let identifierString = self.sheetSelectedDetentIdentifier
else { return nil };

return UISheetPresentationController.Detent.Identifier(
fromString: identifierString
);
};

// MARK: - Properties: Synthesized
// -------------------------------

Expand Down Expand Up @@ -412,7 +482,6 @@ public class RNIModalView:

oldModalContentWrapper.cleanup();
self.modalContentWrapper = nil;

self.deinitControllers();
};

Expand Down Expand Up @@ -489,14 +558,27 @@ public class RNIModalView:
};
};

@available(iOS 15.0, *)
private func sheetAnimateChangesIfNeeded(_ block: () -> Void){
guard let sheetController = self.sheetPresentationController
else { return };

if self.sheetShouldAnimateChanges {
sheetController.animateChanges {
block();
};

} else {
block();
};
};

@available(iOS 15.0, *)
private func applyModalSheetProps(
to sheetController: UISheetPresentationController
){

if let detents = self.synthesizedModalSheetDetents,
detents.count >= 1 {

if let detents = self.synthesizedModalSheetDetents, detents.count >= 1 {
sheetController.detents = detents;
};

Expand All @@ -510,6 +592,16 @@ public class RNIModalView:
self.sheetWidthFollowsPreferredContentSizeWhenEdgeAttached;

sheetController.prefersGrabberVisible = self.sheetPrefersGrabberVisible;

sheetController.largestUndimmedDetentIdentifier =
self.synthesizedSheetLargestUndimmedDetentIdentifier;

if let cornerRadius = self.sheetPreferredCornerRadius?.doubleValue {
sheetController.preferredCornerRadius = cornerRadius;
};

sheetController.selectedDetentIdentifier =
self.synthesizedSheetSelectedDetentIdentifier;
};

// MARK: - Functions - Public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ @interface RCT_EXTERN_MODULE(RNIModalViewManager, RCTViewManager)
RCT_EXPORT_VIEW_PROPERTY(sheetPrefersEdgeAttachedInCompactHeight, BOOL);
RCT_EXPORT_VIEW_PROPERTY(sheetWidthFollowsPreferredContentSizeWhenEdgeAttached, BOOL);
RCT_EXPORT_VIEW_PROPERTY(sheetPrefersGrabberVisible, BOOL);
RCT_EXPORT_VIEW_PROPERTY(sheetShouldAnimateChanges, BOOL);

RCT_EXPORT_VIEW_PROPERTY(sheetLargestUndimmedDetentIdentifier, NSString);
RCT_EXPORT_VIEW_PROPERTY(sheetSelectedDetentIdentifier, NSString);
RCT_EXPORT_VIEW_PROPERTY(sheetPreferredCornerRadius, NSNumber);

@end

9 changes: 8 additions & 1 deletion src/components/ModalView/ModalView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export class ModalView extends
sheetPrefersEdgeAttachedInCompactHeight,
sheetWidthFollowsPreferredContentSizeWhenEdgeAttached,
sheetPrefersGrabberVisible,
sheetShouldAnimateChanges,
sheetLargestUndimmedDetentIdentifier,
sheetPreferredCornerRadius,
sheetSelectedDetentIdentifier,

// Native Props - Events
onModalWillPresent,
Expand Down Expand Up @@ -194,6 +198,10 @@ export class ModalView extends
sheetPrefersEdgeAttachedInCompactHeight,
sheetWidthFollowsPreferredContentSizeWhenEdgeAttached,
sheetPrefersGrabberVisible,
sheetShouldAnimateChanges,
sheetLargestUndimmedDetentIdentifier,
sheetPreferredCornerRadius,
sheetSelectedDetentIdentifier,
onModalWillPresent,
onModalDidPresent,
onModalWillDismiss,
Expand All @@ -210,7 +218,6 @@ export class ModalView extends
onPresentationControllerDidDismiss,
onPresentationControllerDidAttemptToDismiss,


// C - View-Related Props
children,
viewProps,
Expand Down
4 changes: 4 additions & 0 deletions src/components/ModalView/ModalViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export type ModalViewBaseProps = Partial<
| 'sheetPrefersEdgeAttachedInCompactHeight'
| 'sheetWidthFollowsPreferredContentSizeWhenEdgeAttached'
| 'sheetPrefersGrabberVisible'
| 'sheetShouldAnimateChanges'
| 'sheetLargestUndimmedDetentIdentifier'
| 'sheetPreferredCornerRadius'
| 'sheetSelectedDetentIdentifier'

// props - events
| 'onModalWillPresent'
Expand Down
14 changes: 14 additions & 0 deletions src/native_components/RNIModalView/RNIModalViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
OnPresentationControllerDidDismissEvent,
OnPresentationControllerDidAttemptToDismissEvent,
} from './RNIModalViewEvents';
import type { UnionWithAutoComplete } from 'src/types/UtilityTypes';

export type RNIModalViewBaseProps = {
// Props - General
Expand Down Expand Up @@ -64,6 +65,19 @@ export type RNIModalViewBaseProps = {
sheetPrefersEdgeAttachedInCompactHeight?: boolean;
sheetWidthFollowsPreferredContentSizeWhenEdgeAttached?: boolean;
sheetPrefersGrabberVisible?: boolean;
sheetShouldAnimateChanges?: boolean;

sheetPreferredCornerRadius?: number;

sheetLargestUndimmedDetentIdentifier?: UnionWithAutoComplete<
TUISheetPresentationControllerDetents,
string
>;

sheetSelectedDetentIdentifier?: UnionWithAutoComplete<
TUISheetPresentationControllerDetents,
string
>;

// Props - Events
// --------------
Expand Down

0 comments on commit d7f2520

Please sign in to comment.