0.3.46 • Published 4 days ago

@bambooapp/bamboo-molecules v0.3.46

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days ago

Bamboo Molecules

Goals

  • A feature complete UI component library for react-native; works on Android, iOS and Web
  • Follows platform specific guidelines
  • Reliably performant React Native components
  • Easy theming
  • Rapid development
  • Composable components

Core Principles

  • At it core, Bamboo molecules extends the principles of Bamboo Atoms.
    • Dependency Injection\ Allows the product developers to replace a component with an equivalent component
    • Absolute control and customizations\ As a developer, you take control of how Molecules work for you from styles to functionality.
  • A product developer should be able to style all the components, not only colors, but also shapes, sizes and variants.
  • A product developer shouldn't have to worry about importing a multitude of components.
  • A product developer should be able to replace any components, including child components, as he thinks fit.
  • A product developer should be able to use a component without worrying about performance of the component.

What is Bamboo Molecules?

A one place library for React Native Material UI and iOS Cupertino components.\ It aims to provide a solid set of components that can used to create complex platform specific layouts like screens, modals, drawers and navigation.

What does it provide?

Molecules is a set of composed components, a level higher than the un-opinionated Atoms. Following the design pattern, Molecules like Bamboo Atoms; the library is a highly performant, and well tested set of components. Components have been specifically designed to meet the design guidelines of each platform iOS or Android. Bamboo Molecules are opinionated for the platform they cater to.

For the web, the user gets the option to toggle between Material UI and iOS Cupertino style. Molecules components are designed and optimized for the best user experience on the web in both the design styles. Of-course, you can overwrite the styles for any of the platforms using the platform specific extensions. :)

Molecules also exposes an assortment of hooks otherwise not available from bamboo-atoms that make the development experience a breeze.

What does it not do?

Bamboo molecules do not provide a for complex screens and layouts; you can create your own UI layouts using the molecules.

Platforms

  • Android
  • iOS
  • Web

Themes

  • Android
  • iOS
  • Web (Android/iOS)

Getting Started

Installation

Simply get started by using the Bamboo Molecules starter template. Alternately, add Bamboo Molecules to an existing project by installing.

yarn add @webbeetechnologies/bamboo-molecules

Usage

Basic usage

  • ProvideMolecules: React context provider to add the theming and components to your project.
  • useMolecules: Hook to read the provided components from context.
import { ProvideMolecules, useMolecules } from "@webbeetechnologies/bamboo-molecules";

const DemoComponent = () => {
  const {View, Text} = useMolecules();
  return (
    <View>
      <Text>Hello World!</Text>
    </View>
  )
}

const App = (props) => {
  return (
    <ProvideMolecules>
      <DemoComponent />
    </ProvideMolecules>
  )
}

export default Component;

Providing Custom Components

Want to provide custom components to use within your app? Simply pass them to provider. useMolecules is a generic, and thus, and thus it will accept an interface and for type inference.

import { ProvideMolecules, useMolecules } from "@webbeetechnologies/bamboo-molecules";

const components = {
  AwesomeStringComponent: (props: { value: string, onChange?: (value: string) => {} }) => <>{ value }</>,
  AwesomeNumberComponent: (props: { value: number, onChange?: (value: number) => {} }) => <>{ value }</>,
}

const useAwesomeAppComponents = () => useMolecules<typeof components>();


const DemoComponent = () => {
  const {View, AwesomeStringComponent, AwesomeNumberComponent} = useAwesomeAppComponents();
  return (
    <View>
      <AwesomeStringComponent value="Hello World" />
      <AwesomeNumberComponent value={42} />
    </View>
  )
}

const App = (props) => {
  return (
    <ProvideMolecules components={components}>
      <DemoComponent />
    </ProvideMolecules>
  )
}
export default App;

Theming

Want to provide a custom theme or extend the existing components; easy.\ Make a custom style definition for your components by using the extendTheme function. Bamboo molecules implement platform specific design tokens that can be also be easily.

  • extendTheme: accepts a custom default style definition for the Molecules and custom components that extends the theme.
import { ProvideMolecules, useMolecules, extendTheme } from "@webbeetechnologies/bamboo-molecules";

const theme = extendTheme({
  AwesomeStringComponent: {
    color: "colors.onPrimary",
    backgroundColor: "colors.primary",
  },
  Button: {
    backgroundColor: "colors.primary",
    text: "colors.onPrimary",
    states: {
      disabled: {
        backgroundColor: "red",
        text: "black",
      },
      hovered: {
        backgroundColor: "colors.primaryOnHover",
      }
    }
  }
});


const DemoComponent = () => {
  const {Button} = useMolecules();
  return (
    <Button />
  )
}

const App = (props) => {
  return (
    <ProvideMolecules theme={theme}>
      <DemoComponent />
    </ProvideMolecules>
  )
}
export default App;

Styling Custom components

If you are a library developer, you may want to enable your component consumers to extend the styles of the components. You may have different states, you may also want to have different variants. Bamboo Molecules enables you to create a configurable component all the parts of which can be separately styled.

import { FC, useMemo } from "react";
import type { ViewProps } from "react-native";
import { ProvideMolecules, useMolecules, useComponentStyles, extendTheme, } from "@webbeetechnologies/bamboo-molecules";

const theme = extendTheme({
  ChessTile: {
    height: 16,
    width: 16,
    variants: {
      "black": { backgroundColor: "#000" },
      "white": { backgroundColor: "#fff" },
    },
    states: {
      possible: {
        variants: {
          "black": { backgroundColor: "#222" },
          "white": { backgroundColor: "#ddd" },
        }
      },
      selected: {
        variants: {
          "black": { backgroundColor: "#111" },
          "white": { backgroundColor: "#eee" },
        }
      },
      potentialMove: {
        backgroundColor: "blue",
      }
    },
  },
});


const ChessTile: FC<ViewProps & AndSomeChessPositionArgs> = (props) => {
  const { possible, selected, potentialMove } = deriveCurrentTileState(props);
  const componentStyles = useComponentStyles("ChessTile", props.style, {
    // Represents a possible position for the selected piece
    possible,
    // Represents the position of the selected piece
    selected,
    // Represents the selected possible position of the selected piece.
    potentialMove
  });

  const { View, Text } = useMolecules();
  return <View { ...props} />
}

const components = {AwesomeViewComponent}

const App = (props) => {
  const board = useMemo(() => {
    Array.from({ length: 12 }, (_, i) => Array.from({ length: 12 }, (_, j) => <ChessTile variant={ i % 2 === j % 2 ? "white" : 'black' } />))
  }, []);
  
  return (
    <ProvideMolecules components={components} theme={theme}>
      {board}
    </ProvideMolecules>
  )
}
export default App;

Resolve Component styles

Though the component theme is opinionated by default. You can write your own custom style resolver allowing you to use styles you already have. Molecules provider accepts an optional resolveComponentStyles prop.

import { ProvideMolecules, extendTheme, resolveComponentStyles, extractComponentStyles } from "@webbeetechnologies/bamboo-molecules";


const theme = extendTheme({
  Knight: {
    height: 16,
    width: 16,
  },
});

const resolveStyles = (arg) => {
  const {componentName, componentTheme, variant, states, size, style, } = arg;

  switch(componentName) {
    case 'Knight':
      // Do something amazing with the componentTheme.
      // here, you can do something with the variants, states and sizes also.
      // you may choose to drop styles entirely for example.
      break;
    default:
      return resolveComponentStyles(arg);
  }
}

const extractStyles = (arg) => {
  const { theme, componentName, colorMode, style } = arg;

  switch(componentName) {
    case 'Knight':
      // here, you get the theme with duly resolved design tokens.
      // Do something with the style object and the theme.
      break;
    default:
      return resolveComponentStyles(arg);
  }
}

const App = (props) => {
  return (
      <ProvideMolecules theme={theme} extractComponentStyles={extractStyles} resolveComponentStyles={resolveStyles} />
  )
}

Nesting ProvideMolecules - Components

If you need to provide some components access in only a certain part of the application, you can nest ProvideMolecules. ProvideMolecules takes the top-down approach, thus allowing you to overwrite the components of molecules. This also allows you to provide the components at the topmost level, or at a much lower level where the component is most relevant.

Consider the example

import { ProvideMolecules, extendTheme, useMolecules } from "@webbeetechnologies/bamboo-molecules";
import { useMemo } from "react";


const componentsModal = {
  ModalTitle: (props: TextProps) => {
    const { Text } = useMolecules();
    const style = useMemo(() => [{ typescale: 'typescale.displayMedium' }], []);
    return <Text style={style}></Text>
  },
  ModalBody: (props: ViewProps) => {
    const { View } = useMolecules();
    const viewStyles = useMemo(() => [{ borderRadius: 'shapes.corner.large' }], []);
    const textStyles = useMemo(() => [{ typescale: 'typescale.displayRegular' }], []);

    return (
      <View style={viewStyles} {...props}>
        <Text style={textStyles}>{props.children}</Text>
      </View>
    )
  },
}

const componentsRoot = {
  Modal: (props: ViewProps) => {
    const { View } = useMolecules();
    return (
      <ProvideMolecules components={componentsModal}>
        <View {...props} />
      </ProvideMolecules>
    )
  },
  ModalTitle: (props: TextProps) => {
    const { Text } = useMolecules();
    const style = useMemo(() => [{ typescale: 'typescale.headlineLarge' }], []);
    return <Text style={style}></Text>
  },
}

const LibraryModalBody = () => {
  const {ModalBody, ModalTitle} = useMolecules();
  return (
      <>
        <ModalTitle>I am rendered `headlineLarge`</ModalTitle>
        <ModalBody>Some Modal Body</ModalBody>
      </>
  )
}

const AppModal = () => {
  const {Modal} = useMolecules();
  return (
    <Modal>
      <LibraryModalBody />
    </Modal>
  )
}



const App = (props) => {
  return (
          <ProvideMolecules components={componentsRoot}>
            <AppModal />
          </ProvideMolecules>
  )
}

Nesting ProvideMolecules - Themes

You may also find it useful to provide styling components separately at a lower level while still being able to extend the styles from a higher level. While it allows you to create custom styles, it enables the end consumer of these components to overwrite the design. Also, the component consumer can have a different color mode in different parts of the application without much ado.

import { ProvideMolecules, extendTheme, useMolecules } from "@webbeetechnologies/bamboo-molecules";

const theme = extendTheme({
  ViewComponent: {
    backgroundColor: "colors.primary",
    padding: "spacings.16",
    margin: "spacings.16",
  }
})

const darkTheme = extendTheme({
  ...theme,
  colorMode: "dark",
})


const ViewComponent = (props) => {
  const {View, Text} = useMolecules();
  return (
          <View { ...props }>
            <Text>{props.children}</Text>
          </View>
  )
}

const App = (props) => {
  return (
          <ProvideMolecules theme={theme}>
            <ViewComponent>I am in a light theme</ViewComponent>
            <ProvideMolecules theme={darkTheme}>
              <ViewComponent>I am in a dark theme</ViewComponent>
            </ProvideMolecules>
          </ProvideMolecules>
  )
}

Components and hooks

Bamboo Molecules components are well documented in storybooks. Learn how to use Bamboo Molecules in your React Native project now.

Scripts

# Get started with storybooks
yarn start

# Demo project
yarn demo
0.3.46

4 days ago

0.3.45

14 days ago

0.3.44

15 days ago

0.3.43

21 days ago

0.3.42

1 month ago

0.3.41

2 months ago

0.3.40

2 months ago

0.3.38

3 months ago

0.3.37

3 months ago

0.3.36

3 months ago

0.3.35

3 months ago

0.3.34

3 months ago

0.3.33

4 months ago

0.3.31

5 months ago

0.3.30

5 months ago

0.3.32

5 months ago

0.3.29

5 months ago

0.3.28

5 months ago

0.3.27

5 months ago

0.3.26

5 months ago

0.3.20

5 months ago

0.3.25

5 months ago

0.3.24

5 months ago

0.3.23

5 months ago

0.3.22

5 months ago

0.3.21

5 months ago

0.3.19

5 months ago

0.3.18

5 months ago

0.3.17

5 months ago

0.3.16

5 months ago

0.2.27

8 months ago

0.2.24

8 months ago

0.2.23

8 months ago

0.2.22

9 months ago

0.2.21

9 months ago

0.2.20

9 months ago

0.2.19

9 months ago

0.2.18

9 months ago

0.2.10-dev.0

9 months ago

0.2.17

9 months ago

0.1.73-rc.0

9 months ago

0.2.16

9 months ago

0.2.15

9 months ago

0.2.14

9 months ago

0.2.11-dev.0

9 months ago

0.2.13

9 months ago

0.2.11-dev.1

9 months ago

0.2.12

9 months ago

0.2.11

9 months ago

0.2.10

9 months ago

0.3.6

6 months ago

0.3.5

6 months ago

0.3.7

6 months ago

0.3.4

6 months ago

0.3.3

6 months ago

0.3.1-dev.1

6 months ago

0.3.1-dev.0

7 months ago

0.2.9-dev.0

9 months ago

0.2.61-dev.0

7 months ago

0.2.74

7 months ago

0.2.73

7 months ago

0.2.72

7 months ago

0.2.71

7 months ago

0.2.70

7 months ago

0.2.79

6 months ago

0.2.78

7 months ago

0.2.77

7 months ago

0.2.76

7 months ago

0.2.75

7 months ago

0.2.59-dev.0

7 months ago

0.2.59-dev.1

7 months ago

0.2.63

7 months ago

0.2.62

7 months ago

0.2.61

7 months ago

0.2.60

7 months ago

0.2.8-dev.0

9 months ago

0.2.69

7 months ago

0.2.68

7 months ago

0.2.67

7 months ago

0.2.66

7 months ago

0.2.65

7 months ago

0.1.72-rc.0

9 months ago

0.2.64

7 months ago

0.2.52

7 months ago

0.2.51

7 months ago

0.2.50

7 months ago

0.2.59

7 months ago

0.2.58

7 months ago

0.2.57

7 months ago

0.2.56

7 months ago

0.2.54

7 months ago

0.2.53

7 months ago

0.2.41

8 months ago

0.2.40

8 months ago

0.3.9

6 months ago

0.2.67-dev.1

7 months ago

0.2.49

7 months ago

0.2.48

7 months ago

0.3.15

5 months ago

0.2.47

7 months ago

0.3.14

5 months ago

0.2.46

8 months ago

0.3.13

5 months ago

0.2.45

8 months ago

0.1.70

9 months ago

0.3.12

6 months ago

0.2.44

8 months ago

0.2.12-dev.0

9 months ago

0.1.71

9 months ago

0.3.11

6 months ago

0.1.72

9 months ago

0.3.10

6 months ago

0.2.42

8 months ago

0.1.73

9 months ago

0.2.39

8 months ago

0.2.30

8 months ago

0.1.63

10 months ago

0.1.64

10 months ago

0.1.65

9 months ago

0.1.66

9 months ago

0.1.67

9 months ago

0.1.69

9 months ago

0.2.38

8 months ago

0.2.37

8 months ago

0.2.36

8 months ago

0.2.35

8 months ago

0.2.34

8 months ago

0.2.33

8 months ago

0.2.32

8 months ago

0.1.61

10 months ago

0.1.62

10 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.2.29

8 months ago

0.2.28

8 months ago

0.2.7

9 months ago

0.2.6

9 months ago

0.2.8

9 months ago

0.2.3

9 months ago

0.3.3-dev.2

6 months ago

0.3.3-dev.1

6 months ago

0.2.5

9 months ago

0.3.3-dev.0

6 months ago

0.2.4

9 months ago

0.1.55

10 months ago

0.1.56

10 months ago

0.1.57

10 months ago

0.1.58

10 months ago

0.1.59

10 months ago

0.1.60

10 months ago

0.1.52

11 months ago

0.1.53

11 months ago

0.1.54

11 months ago

0.1.50

11 months ago

0.1.51

11 months ago

0.1.49

11 months ago

0.1.41

12 months ago

0.1.42

12 months ago

0.1.43

12 months ago

0.1.44

12 months ago

0.1.45

12 months ago

0.1.46

12 months ago

0.1.47

12 months ago

0.1.48

11 months ago

0.1.40

12 months ago

0.1.39

12 months ago

0.1.35

1 year ago

0.1.36

1 year ago

0.1.37

1 year ago

0.1.38

1 year ago

0.1.30

1 year ago

0.1.31

1 year ago

0.1.20

1 year ago

0.1.32

1 year ago

0.1.21

1 year ago

0.1.33

1 year ago

0.1.22

1 year ago

0.1.23

1 year ago

0.1.24

1 year ago

0.1.26

1 year ago

0.1.27

1 year ago

0.1.28

1 year ago

0.1.17

1 year ago

0.1.29

1 year ago

0.1.18

1 year ago

0.1.19

1 year ago

0.1.16

1 year ago

0.1.15

1 year ago

0.1.14

1 year ago