2.0.0 • Published 3 months ago

react-native-floating-action-menu v2.0.0

Weekly downloads
38
License
MIT
Repository
github
Last release
3 months ago

Installation

npm install --save react-native-floating-action-menu

Usage

import { FloatingMenu } from 'react-native-floating-action-menu';

const items = [
  { label: 'Do a little dance' },
  { label: 'Make a lil love' },
  { label: 'Get down tonight' },
];

<FloatingMenu
  items={items}
  isOpen={this.state.isMenuOpen}
  onMenuToggle={this.handleMenuToggle}
  onItemPress={this.handleItemPress}
/>

Item Config

FloatingItem

Propdescriptiontyperequired
labelText to display alongside buttonstring✔︎
labelStyleStyle for the Text elementobject
isPendingWill display ActivityIndicator in place of icon when isPending is trueboolean
isDisabledWill disable the item when isDisabled is trueboolean
onPressCallback function called when this item is pressed. This will override the default onItemPress callback given to FloatingMenufunction

Example:

{
  label: 'Hello world',
  isPending: false,
  isDisabled: false,
  onPress: (item, index) => {}, // (optional, can also be handled via `onItemPress`)
  // Anything else you want goes here
}

Menu Config

Propdescriptiontypedefault
itemsArray of Items (See above). Items are positioned by their order in this array and start closest to the menu button.FloatingItem[][]
isOpenControl the menu open/closed statebooleanfalse
position"top-left" | "top-right" | "bottom-left" | "bottom-right"string"bottom-right"
topPosition in px away from top edgenumber38
leftPosition in px away from left edgenumber38
rightPosition in px away from right edgenumber38
bottomPosition in px away from bottom edgenumber38
primaryColorHex color string used for backgrounds, borders, and iconsstring"#213A77"
backgroundUpColorOverride background color for menu and items UP state. Defaults to #ffffff.string (hex)-
backgroundDownColorOverride background color for menu and items DOWN state. Defaults to primaryColor value.string (hex)-
borderColorOverride border color for menu and items. Defaults to primaryColor value.string (hex)-
iconColorOverride icon color for menu and items. Defaults to primaryColor value.string (hex)-
buttonWidthWidth (and also height) of the buttonnumber50
innerWidthWidth (and also height) of the inner element of the buttonnumber-
dimmerStyleStyle the background dimmer elementobject-
openEaseEasing function used for the opening animation (see js easing functions)functiont => (--t) * t * t + 1
closeEaseEasing function used for the closing animation (see js easing functions)functiont => t t t
renderMenuIconFunction used to render the icon for menu button. Receives current menu state as an argument. (see below example)function-
renderItemIconFunction used to render the icon for the items. Receives item, index, and current menu state as arguments. (see below example)function-
onMenuToggleCallback function called when the menu has been toggled open or closed. Receives a boolean valuefunction-
onItemPressCallback function called when a menu item has been pressed. Receives item and index. If an item specifies its own onPress function, it will take priority, and this function will be ignoredfunction-

Gif Demos

Quick Start Example

import React from 'react';
import { StyleSheet } from 'react-native';
import { FloatingMenu } from 'react-native-floating-action-menu';

const items = [
  { label: 'Do a little dance' },
  { label: 'Make a lil love' },
  { label: 'Get down tonight' },
];

class MyScreen extends React.Component {
  state = {
    isMenuOpen: false,
  };

  handleMenuToggle = isMenuOpen =>
    this.setState({ isMenuOpen });

  handleItemPress = (item, index) =>
    console.log('pressed item', item);

  render() {
    return (
      <View style={styles.container}>
        <FloatingMenu
          isOpen={this.state.isMenuOpen}
          items={items}
          onMenuToggle={this.handleMenuToggle}
          onItemPress={this.handleItemPress}
        />
      </View>
    );
  }
};

const styles = StyleSheet.create({
  container: {
    width: '100%',
    height: '100%',
    position: 'relative',
  },
});

export default MyScreen;

Example rendering icons (FontAwesome, regular Images)

import { Image } from 'react-native';
import { FloatingMenu } from 'react-native-floating-action-menu';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { faBars, faTimes, faUserPlus } from '@fortawesome/free-solid-svg-icons';

// Specify data required to render the icon
const items = [
  {
    label: 'First is an icon',
    fa: faUserPlus
  },
  {
    label: 'Second is an image',
    image: require('../assets/img-0.png')
  },
];
// Optional color to be silly
const primaryColor = '#09f';

class MyScreen extends React.Component {
  state = {};

  renderMenuIcon = (menuState) => {
    const { menuButtonDown } = menuState;

    return menuButtonDown
      ? <Image source={require('./btn-down.png')} />
      : <Image source={require('./btn-up.png')} />;
  }

  renderItemIcon = (item, index, menuState) => {
    const { itemsDown, dimmerActive } = menuState;

    const isItemPressed = itemsDown[index];
    const color = isItemPressed ? '#fff' : primaryColor;

    // Icons can be rendered however you like.
    // Here are some examples, using data from the item object:

    if (item.fa) {
      return (
        <FontAwesomeIcon
          icon={item.fa}
          size={25}
          color={color}
        />
      );
    }
    else if (item.image) {
      return (
        <Image
          source={item.image}
          style={{ tintColor: color }}
          resizeMode="contain"
        />
      );
    }

    return null;
  };

  ...

Run Example

  • git clone https://github.com/nicotroia/react-native-floating-action-menu
  • cd react-native-floating-action-menu/example
  • npm install
  • npm run ios # or android

Develop

  • npm pack
  • cd example
  • npm install ../react-native-floating-action-menu.tgz --save
  • npm run ios # or android

License

MIT © 2019-2024 internet-nico

2.0.0

3 months ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.11

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago