template with nextts, eslint&prettier, husky&lint-staged, tailwindcss&styled-components

Overview

Setup

  1. npm i
  2. Use VSCode, make sure the recommended eslint and prettier plugins are installed. Automatic linting should occur when you save!
  3. npx husky install. Automatic linting should occur when you commit!

Commands

  • npm run dev: Runs the local Next.js dev server.
  • npm run build: Generates the Next.js production build.
  • npm start: Starts the Next.js production build.

Steps to reproduce template

Note that the actual commits in this repo do not exactly reflect these steps in the same order

npx create-next-app --ts

Add "baseUrl": "." to "compilerOptions" in tsconfig.json

Add to .eslintrc.json:

"rules": {
  "eqeqeq": "error"
}

Linting stuff:

In folder .vscode, create settings.json:

{
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
}

And extensions.json:

{
  "recommendations": ["dbaeumer.vscode-eslint"]
}

npm i -D @typescript-eslint/eslint-plugin, add "plugin:@typescript-eslint/recommended" to "extends" in .eslintrc.json

npm i -D prettier eslint-config-prettier, add "prettier" to "extends" in .eslintrc.json

Add to settings.json:

"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"

Add "esbenp.prettier-vscode" to extensions.json

Create .prettierrc.json:

{
  "singleQuote": true
}

Create .prettierignore:

.next

npm i -D husky lint-staged, npx husky install, npx husky add .husky/pre-commit "npx lint-staged"

Create lint-staged.config.js:

module.exports = {
  '**/*.(ts|tsx)': () => 'npx tsc --noEmit',
  '**/*.(ts|tsx|js)': (filenames) => [
    `npx eslint --fix ${filenames.join(' ')}`,
    `npx prettier --write ${filenames.join(' ')}`,
  ],
  '**/*.(md|json)': (filenames) =>
    `npx prettier --write ${filenames.join(' ')}`,
};

Twin styled-components:

npm i styled-components, npm i -D twin.macro tailwindcss babel-plugin-macros @types/styled-components

Create components/GlobalStyles.tsx:

import { createGlobalStyle } from 'styled-components';
import { GlobalStyles as BaseStyles } from 'twin.macro';

const CustomStyles = createGlobalStyle`
  body {}
`;

const GlobalStyles = () => (
  <>
    <BaseStyles />
    <CustomStyles />
  </>
);

export default GlobalStyles;

Modify pages/_app.tsx:

import GlobalStyles from 'components/GlobalStyles';
import type { AppProps } from 'next/app';

const App = ({ Component, pageProps }: AppProps) => (
  <div>
    <GlobalStyles />
    <Component {...pageProps} />
  </div>
);

export default App;

Create babel-plugin-macros.config.js:

module.exports = {
  twin: {
    preset: 'styled-components',
    autoCssProp: false,
  },
};

Create .babelrc.js:

module.exports = {
  presets: [['next/babel', { 'preset-react': { runtime: 'automatic' } }]],
  plugins: [
    'babel-plugin-macros',
    ['babel-plugin-styled-components', { ssr: true }],
  ],
};

Create _document.tsx:

import Document, { DocumentContext } from 'next/document';
import { ServerStyleSheet } from 'styled-components';

export default class MyDocument extends Document {
  static async getInitialProps(ctx: DocumentContext) {
    const sheet = new ServerStyleSheet();
    const originalRenderPage = ctx.renderPage;
    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: (App) => (props) =>
            sheet.collectStyles(<App {...props} />),
        });
      const initialProps = await Document.getInitialProps(ctx);

      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        ),
      };
    } finally {
      sheet.seal();
    }
  }
}

Typescript does not like twin.macro and styled-components together for some reason, I searched a bit and it seems like creating types/twin.d.ts solves the problem (IDK why, from here):

import 'twin.macro';
import styledImport, { CSSProp, css as cssImport } from 'styled-components';

declare module 'twin.macro' {
  // The styled and css imports
  const styled: typeof styledImport;
  const css: typeof cssImport;
}

declare module 'react' {
  // The css prop
  interface HTMLAttributes<T> extends DOMAttributes<T> {
    css?: CSSProp;
    tw?: string;
  }
  // The inline svg css prop
  interface SVGProps<T> extends SVGProps<SVGSVGElement> {
    css?: CSSProp;
    tw?: string;
  }
}

// The 'as' prop on styled components
declare global {
  namespace JSX {
    interface IntrinsicAttributes<T> extends DOMAttributes<T> {
      as?: string;
    }
  }
}

Removed styles/ for our own styling system!

You might also like...

Nextjs-components: A collection of React components

Nextjs-components: A collection of React components

nextjs-components A collection of React components, transcribed from https://vercel.com/design. 1 Motivation Blog post from 01/09/2022 Todo's Unit tes

Nov 30, 2022

This is my portfolio GitHub clone website. The frontend is build using NextJS and TailwindCSS.

This is my portfolio GitHub clone website. The frontend is build using NextJS and TailwindCSS.

Github Clone Portfolio Website Tech Stack used: NextJS Tailwind CSS The contact form in this website is connected to Notion. If you want to integrate

Oct 5, 2022

trying to clone tiktok using next, typescript, tailwindcss and hasura graphql. For educational purposes only

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Jan 4, 2022

React + TailwindCSS 3.0

React + TailwindCSS 3.0

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Dec 25, 2021

Blog - ⚡️ Blog pessoal onde publico artigos relacionados a dev. Desenvolvido com Next.js e TailwindCSS

Blog - ⚡️ Blog pessoal onde publico artigos relacionados a dev. Desenvolvido com Next.js e TailwindCSS

⚡️ Blog Blog pessoal desenvolvido com Next.js e TypeScript ℹ️ Sobre o projeto Este projeto consiste em um blog pessoal, onde futuramente farei posts d

Oct 6, 2022

Personal website and blog made using NextJS, TailwindCSS, GraphCMS, and MDX bundler

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Aug 21, 2022

ESLint plugin for react-hook-form

eslint-plugin-react-hook-form react-hook-form is an awsome library which provide a neat solution for building forms. However, there are many rules for

Nov 22, 2022

A custom ESLint rule to allow static deps in React Hooks ⚛️

eslint-plugin-react-hooks-static-deps A custom ESLint rule to allow static deps in React Hooks ⚛️ Motivation react-hooks/exhaustive-deps is a really n

Apr 5, 2022

📓 The UI component explorer. Develop, document, & test React, Vue, Angular, Web Components, Ember, Svelte & more!

📓 The UI component explorer. Develop, document, & test React, Vue, Angular, Web Components, Ember, Svelte & more!

Build bulletproof UI components faster Storybook is a development environment for UI components. It allows you to browse a component library, view the

Jan 4, 2023
Owner
Competitive Programming Lover IOI 20 Winner MIT '24
null
Terminal Styled Portfolio Website ˋ( ° ▽、° ) , a terminal style/styled portfolio website made with <3 using react.

AshTerm A Terminal Styled Portfolio Website. ??‍?? Made Using- ⚛ Framework ReactJS ?? Terminal react-console-emulator ?? Deployed using CloudFlare Run

ashish 67 Nov 22, 2022
:atom_symbol: React primitive UI components built with styled-system.

Rebass React primitive UI components built with Styled System. https://rebassjs.org npm i rebass Getting Started import React from 'react' import { Bo

Rebass 7.9k Dec 31, 2022
NetflixClone - Netflix clone with react , styled components and firebase with user authentication.........

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 1 Jan 3, 2022
Boilerplate de projeto nextjs, com Styled Components e Typescript

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Fabi Rodrigues 2 Mar 23, 2022
A react component available on npm to easily link to your project on github and is made using React, TypeScript and styled-components.

fork-me-corner fork-me-corner is a react component available on npm to easily link to your project on github and is made using React, TypeScript and s

Victor Dantas 9 Jun 30, 2022
This is best boilerplate project using Next.js, Typescript, Redux, Styled-component.

GL-Next boilerplate Start your next Next project in seconds A highly scalable, offline-first foundation with the best DX and a focus on performance an

GOLD LION 1 Sep 1, 2022
Prettier formatting for ?_trace=1 traces

datasette-pretty-traces Prettier formatting for ?_trace=1 traces Installation Install this plugin in the same environment as Datasette. $ datasette in

Simon Willison 4 May 26, 2022
A React utility belt for function components and higher-order components.

A Note from the Author (acdlite, Oct 25 2018): Hi! I created Recompose about three years ago. About a year after that, I joined the React team. Today,

Andrew Clark 14.8k Jan 4, 2023
we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

고스락 6 Aug 12, 2022
Providing accessible components with Web Components & Material You

tiny-material Still on developing, DO NOT use for production environment Run well on Google Chrome, Firefox, Chrome for Android, Microsoft Edge (Chrom

HugePancake 11 Dec 31, 2022