Skip to content

feat: Add support for setting z-index #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/ToastUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

export type ToastUIProps = {
isVisible: boolean;
options: Required<ToastOptions>;
options: ToastOptions;
data: ToastData;
show: (params: ToastShowParams) => void;
hide: (params: ToastHideParams) => void;
Expand Down Expand Up @@ -65,7 +65,7 @@ function renderComponent({

export function ToastUI(props: ToastUIProps) {
const { isVisible, options, hide } = props;
const { position, topOffset, bottomOffset, keyboardOffset } = options;
const { position, topOffset, bottomOffset, keyboardOffset, containerZIndex } = options;

return (
<AnimatedContainer
Expand All @@ -74,6 +74,7 @@ export function ToastUI(props: ToastUIProps) {
topOffset={topOffset}
bottomOffset={bottomOffset}
keyboardOffset={keyboardOffset}
containerZIndex={containerZIndex}
onHide={hide}>
{renderComponent(props)}
</AnimatedContainer>
Expand Down
4 changes: 3 additions & 1 deletion src/components/AnimatedContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type AnimatedContainerProps = {
isVisible: boolean;
position: ToastPosition;
topOffset: number;
containerZIndex?: number;
bottomOffset: number;
keyboardOffset: number;
onHide: () => void;
Expand Down Expand Up @@ -74,6 +75,7 @@ export function AnimatedContainer({
bottomOffset,
keyboardOffset,
onHide,
containerZIndex,
onRestorePosition = noop
}: AnimatedContainerProps) {
const { log } = useLogger();
Expand Down Expand Up @@ -124,7 +126,7 @@ export function AnimatedContainer({
<Animated.View
testID={getTestId('AnimatedContainer')}
onLayout={computeViewDimensions}
style={[styles.base, styles[position], animationStyles]}
style={[styles.base, styles[position], animationStyles, typeof containerZIndex === "number" ? { zIndex: containerZIndex } : {}]}
// This container View is never the target of touch events but its subviews can be.
// By doing this, tapping buttons behind the Toast is allowed
pointerEvents='box-none'
Expand Down
18 changes: 15 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ReactChildren = React.ReactNode;
export type ToastType = string;
export type ToastPosition = 'top' | 'bottom';

export type ToastOptions = {
type ToastOptionsBase = {
/**
* Toast type.
* Default value: `success`
Expand Down Expand Up @@ -54,6 +54,11 @@ export type ToastOptions = {
* Default value: `10`
*/
keyboardOffset?: number;
/**
* z-index of the animated container for the toasts
* Default value: undefined
*/
containerZIndex?: number;
/**
* Called when Toast is shown
*/
Expand All @@ -79,7 +84,9 @@ export type ToastData = {
text2?: string;
};

export type ToastShowParams = ToastData & ToastOptions;
export type ToastOptions = Required<Omit<ToastOptionsBase, "containerZIndex">> & Pick<ToastOptionsBase, "containerZIndex">

export type ToastShowParams = ToastData & Partial<ToastOptions>;

export type ToastHideParams = void;

Expand Down Expand Up @@ -172,6 +179,11 @@ export type ToastProps = {
* Default value: `10`
*/
keyboardOffset?: number;
/**
* z-index of the animated container for the toasts
* Default value: undefined
*/
containerZIndex?: number;
/**
* Called when any Toast is shown
*/
Expand All @@ -184,4 +196,4 @@ export type ToastProps = {
* Called on any Toast press
*/
onPress?: () => void;
};
};
8 changes: 4 additions & 4 deletions src/useToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const DEFAULT_DATA: ToastData = {
text2: undefined
};

export const DEFAULT_OPTIONS: Required<ToastOptions> = {
export const DEFAULT_OPTIONS: ToastOptions = {
type: 'success',
position: 'top',
autoHide: true,
Expand All @@ -38,9 +38,9 @@ export function useToast({ defaultOptions }: UseToastParams) {
const initialOptions = mergeIfDefined(
DEFAULT_OPTIONS,
defaultOptions
) as Required<ToastOptions>;
) as ToastOptions;
const [options, setOptions] =
React.useState<Required<ToastOptions>>(initialOptions);
React.useState<ToastOptions>(initialOptions);

const onAutoHide = React.useCallback(() => {
log('Auto hiding');
Expand Down Expand Up @@ -94,7 +94,7 @@ export function useToast({ defaultOptions }: UseToastParams) {
onHide,
onPress,
props
}) as Required<ToastOptions>
}) as ToastOptions
);
// TODO: validate input
// TODO: use a queue when Toast is already visible
Expand Down