Skip to content

Commit

Permalink
feat: allow unsafe usage for useBottomSheetInternal & useBottomSheetM…
Browse files Browse the repository at this point in the history
…odalInternal (#740)(by @jembach)

* feat: add useBottomSheetInternal_unsafe & useBottomSheetModalInternal_unsafe to access context

* chore: export hooks useBottomSheetInternal_unsafe and useBottomSheetModalInternal_unsafe

* refactor: combined unsafe hooks with default hooks using an argument

* chore(useBottomSheetInternal): use function overloading instead of interface

* chore(useBottomSheetModalInternal): use function overloading instead of interface

* chore: fixed spacing

Co-authored-by: gorhom <gorhom.dev@gmail.com>
  • Loading branch information
jembach and gorhom committed Apr 24, 2022
1 parent 831df9c commit 1bf6139
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/hooks/useBottomSheetInternal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { useContext } from 'react';
import { BottomSheetInternalContext } from '../contexts/internal';
import {
BottomSheetInternalContext,
BottomSheetInternalContextType,
} from '../contexts/internal';

export const useBottomSheetInternal = () => {
export function useBottomSheetInternal(
unsafe?: false
): BottomSheetInternalContextType;

export function useBottomSheetInternal(
unsafe: true
): BottomSheetInternalContextType | null;

export function useBottomSheetInternal(
unsafe?: boolean
): BottomSheetInternalContextType | null {
const context = useContext(BottomSheetInternalContext);

if (context === null) {
if (unsafe !== true && context === null) {
throw "'useBottomSheetInternal' cannot be used out of the BottomSheet!";
}

return context;
};
}
21 changes: 17 additions & 4 deletions src/hooks/useBottomSheetModalInternal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { useContext } from 'react';
import { BottomSheetModalInternalContext } from '../contexts';
import {
BottomSheetModalInternalContext,
BottomSheetModalInternalContextType,
} from '../contexts';

export const useBottomSheetModalInternal = () => {
export function useBottomSheetModalInternal(
unsafe?: false
): BottomSheetModalInternalContextType;

export function useBottomSheetModalInternal(
unsafe: true
): BottomSheetModalInternalContextType | null;

export function useBottomSheetModalInternal(
unsafe?: boolean
): BottomSheetModalInternalContextType | null {
const context = useContext(BottomSheetModalInternalContext);

if (context === null) {
if (unsafe !== true && context === null) {
throw "'BottomSheetModalInternalContext' cannot be null!";
}

return context;
};
}

0 comments on commit 1bf6139

Please sign in to comment.