@moxy/next-button

A react button that doubles down as a link.


Keywords
button, nextjs, react
License
MIT
Install
npm install @moxy/next-button@1.0.0

Documentation

next-button

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status

A react button that doubles down as a link.

Installation

$ npm install @moxy/next-button

This library is written in modern JavaScript and is published in both CommonJS and ES module transpiled variants. If you target older browsers please make sure to transpile accordingly.

Motivation

Every website or app needs a button, so this is our bare-bones version of it. In addition, it doubles down as a NextLink if an href prop is passed, maintaining the style of a button.

Usage

import React from 'react';
import NextButton from '@moxy/next-button';

const MyPage = (props) => (
    <div className="container">
        <NextButton>Button 1</NextButton>
        <NextButton href="/about">Button 2</NextButton>
    </div>
);

export default MyPage;

Import the stylesheet in the project's entry JavaScript file:

import '@moxy/next-button/dist/index.css';

API

Besides the following supported props by the NextButton component, additional props are spread.

children

Type: node | Required: true

What to render inside the component.

onClick

Type: func | Required: false

The onClick event handler.

href

Type: string | Required: false

If this prop is passed, a NextLink will be rendered, maintaining the style of a button. Check out its API to check available props.

className

Type: string | Required: false

A className to apply to the component wrapper.

Tests

$ npm test
$ npm test -- --watch # during development

Demo

A demo Next.js project is available in the /demo folder so you can try this component out.

First, build the next-button project with:

$ npm run build

To run the demo, do the following inside the demo's folder:

$ npm i
$ npm run dev

Note: Everytime a change is made to the package a rebuild is required to reflect those changes on the demo.

FAQ

I can't override the component's CSS, what's happening?

There is an ongoing next.js issue about the loading order of modules and global CSS in development mode. This has been fixed in v9.3.6-canary.0, so you can either update next.js to a version higher than v9.3.5, or simply increase the CSS specificity when overriding component's classes, as we did in the demo, e.g. having the page or section CSS wrap the component's one.

License

Released under the MIT License.