Skip to content

Commit

Permalink
Option to adjust the space above OverflowMenus (#110)
Browse files Browse the repository at this point in the history
* Made spaceAboveMenu customizable

* Moved spaceAboveMenu to OverflowMenuContext to allow for multiple different space settings

* Added spaceAboveMenu as props to OverflowMenuProvider

* Removed space before semicolon

* Resolved errors from checks

* Resolved prettier errors

* Added spaceAboveMenu-info to README.md
  • Loading branch information
dvbeelen committed Feb 1, 2021
1 parent 476e1b6 commit f719b53
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ const HiddenItemWrappedTwice = ()=> <HiddenItemWrapped />
This is a React context provider needed for `overflowMenuPressHandlerDropdownMenu` to work. If you're not using `overflowMenuPressHandlerDropdownMenu` then you don't need it.
By default, you need to wrap your root component with it.

On Android, `OverflowMenuProvider` accepts an optional `spaceAboveMenu` prop, which can be used to set the distance between the top of the screen and the top of the overflow menu.

#### `HeaderButton`

You will typically not use `HeaderButton` directly. `HeaderButton` is where all the `onPress`, `title` and Icon-related props meet to render actual button.
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ declare class Divider extends Component<{

declare class OverflowMenuProvider extends Component<{
children: ReactChild;
spaceAboveMenu?: number;
}> {}

export interface OnOverflowMenuPressParams {
Expand Down
34 changes: 19 additions & 15 deletions src/overflowMenu/OverflowMenuContext.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @flow
import * as React from 'react';
import { Dimensions, Platform } from 'react-native';
import { getDefaultSpaceAboveMenu } from './statusBarUtils';
import { Menu } from './vendor/Menu';
import { getSpaceAboveMenu } from './statusBarUtils';

export type ToggleMenuParam = ?{|
elements: React.ChildrenArray<any>,
Expand All @@ -24,9 +24,10 @@ export const OverflowMenuContext = React.createContext<(ToggleMenuParam) => void

type Props = {|
children: React.Element<any>,
spaceAboveMenu?: number,
|};

export const OverflowMenuProvider = ({ children }: Props) => {
export const OverflowMenuProvider = ({ children, spaceAboveMenu }: Props) => {
const [visible, setVisible] = React.useState(false);
const [position, setPosition] = React.useState({ x: Dimensions.get('window').width - 10, y: 40 });
const [elements, setElements] = React.useState(null);
Expand All @@ -35,19 +36,22 @@ export const OverflowMenuProvider = ({ children }: Props) => {
setVisible(false);
}, []);

const toggleMenu = React.useCallback((params: ToggleMenuParam) => {
setVisible((prevVisible) => !prevVisible);
setElements(params?.elements || []);
if (params) {
const { x, y } = params;
const heightApprox = getSpaceAboveMenu();
const extraDelta = Platform.select({
android: heightApprox,
default: OVERFLOW_TOP,
});
setPosition({ x, y: y + extraDelta });
}
}, []);
const toggleMenu = React.useCallback(
(params: ToggleMenuParam) => {
setVisible((prevVisible) => !prevVisible);
setElements(params?.elements || []);
if (params) {
const { x, y } = params;
const heightApprox = spaceAboveMenu ?? getDefaultSpaceAboveMenu();
const extraDelta = Platform.select({
android: heightApprox,
default: OVERFLOW_TOP,
});
setPosition({ x, y: y + extraDelta });
}
},
[spaceAboveMenu]
);

return (
<OverflowMenuContext.Provider value={toggleMenu}>
Expand Down
2 changes: 1 addition & 1 deletion src/overflowMenu/statusBarUtils.expo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StatusBar } from 'react-native';

export const getSpaceAboveMenu = () => {
export const getDefaultSpaceAboveMenu = () => {
// approximation, expo draws views right from below the display edge
// which is a different behavior from vanilla RN
return StatusBar.currentHeight + 3;
Expand Down
2 changes: 1 addition & 1 deletion src/overflowMenu/statusBarUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const getSpaceAboveMenu = () => {
export const getDefaultSpaceAboveMenu = () => {
return 0;
};

0 comments on commit f719b53

Please sign in to comment.