0.1.3 • Published 5 years ago

menu-system v0.1.3

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

menu-system

Powerful and simple tool to build a complex menu tree.

Usage

Construct menu:

// Create builder
const builder = createMenuBuilder(options?: MenuConfig)

// Create tree
const tree = builder.tree(({ route, arg }) => ({
  home: route(),
  users: route({
    children: {
      id: arg({
        children: {
          comments: route()
        }
      })
    }
  })
}))

// Build tree
const menu = builder.build(tree)

Use it anywhere:

menu.routes.home._.fullpath() // '/home'
menu.routes.users.id._.fullpath({ id: '1' }) // '/users/1'
menu.routes.users.id.comments._.fullpath({ id: 'mc-petry' }) // '/users/mc-petry/comments'

Configuration

interface MenuConfig {
  /**
   * Global routes prefix.
   * @default '/'
   */
  basePath?: string

  /**
   * Use trailing slash on routes
   * @default false
   */
  trailingSlash?: boolean
}

Helpers

/**
 * Returns route that match specified location.
 * Otherwise returns null
 */
menu.findRoute(location: string, maxLevel?: number): Route | null

Routes meta

const menu = builder.build(
  builder.tree(({ route }) => ({
    home: route({
      meta: {
        hidden: true
      }
    }),
    about: route({
      meta: {
        hidden: false
      }
    })
  }))
)

// Example usage
menu.routes._.children.filter(route => !route.meta.hidden)

Split trees

const homeTree = builder.tree(...)
const usersTree = builder.tree(...)

const menu = builder.build({
  ...homeTree,
  ...usersTree
})

Compatibility

All modern environments.\ IE 11 requires polyfills: String.startsWith()

Development

Publishing

npm run build\ npm publish dist

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

6 years ago

0.1.0

6 years ago