<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: amalv</title>
    <description>The latest articles on Forem by amalv (@amalv).</description>
    <link>https://forem.com/amalv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F296737%2F415fc5da-4272-4eba-90e9-baf6d9dfeeba.png</url>
      <title>Forem: amalv</title>
      <link>https://forem.com/amalv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/amalv"/>
    <language>en</language>
    <item>
      <title>A cleaner Github workflow: one commit per Pull Pequest</title>
      <dc:creator>amalv</dc:creator>
      <pubDate>Fri, 01 May 2020 20:09:17 +0000</pubDate>
      <link>https://forem.com/amalv/a-cleaner-github-workflow-one-commit-per-pull-pequest-3ic1</link>
      <guid>https://forem.com/amalv/a-cleaner-github-workflow-one-commit-per-pull-pequest-3ic1</guid>
      <description>&lt;p&gt;In this guide you'll learn how to keep a cleaner Git history having one commit per Pull Request (hereinafter referred to as "PR").&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of a linear Git history
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Easier to do a Git bisect in order to find bugs: find the PR that has the bug more effortlessly because there is a one to one relationship between PRs and commits.&lt;/li&gt;
&lt;li&gt;Git log history will be more readable: since it will be a shorter and concise Git history.&lt;/li&gt;
&lt;li&gt;Commit early and commit often: quick disposable messages in case you want to go back and not start from the beginning when working on a feature or bug fix.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Configure your Github repository to allow rebase merging
&lt;/h2&gt;

&lt;p&gt;The Github merge button for the PRs adds merge commits by default. As a first step update your Github repository settings in order to change this behaviour.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1hjyv06k1dwg7g8tj3t6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1hjyv06k1dwg7g8tj3t6.png" alt="rebase"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Commit early and commit often
&lt;/h2&gt;

&lt;p&gt;If you fail to commit and then do something poorly thought out, you can run into trouble. Additionally, having periodic checkpoints means that you can understand how you broke something.&lt;/p&gt;

&lt;p&gt;Let's say you are trying to add theming support to your app, here is an example of several commits in your feature branch:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

git log --pretty=oneline


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

2ae83a39277d4940ce6d3c76c742047130be4adb (HEAD -&amp;gt; add-theming) Finish
0b47fcbaa276323fc43134652f77ccd49d072e1b WIP
4c2e809097df993ffc57b4c8ce01de1c0042efb2 Fix theming provider
db368b434628d3859c5f36cf7c74d9788af34bdf Add theming support
356351b29c79682393ee10ee6c6a6c97fc678d7b (origin/master, origin/HEAD, master) Initial commit


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  When finished, do a git rebase
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

git rebase -i master


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will prompt your editor to allow an interactive rebase:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pick db368b4 Add theming support
squash 4c2e809 Fix theming provider
squash 0b47fcb WIP
squash 2ae83a3 Finish


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Squash all commits except the first one.&lt;/p&gt;

&lt;p&gt;Save the changes and another screen will appear with your commit messages:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# This is a combination of 4 commits.
# This is the 1st commit message:

Add theming support

# This is the commit message #2:

Fix theming provider

# This is the commit message #3:

WIP

# This is the commit message #4:

Finish


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That you can rewrite it with something similar to this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# This is a combination of 4 commits.
# This is the 1st commit message:

Add theming support

- First step
- Second step
- Third step

# This is the commit message #2:

# Fix theming provider

# This is the commit message #3:

# WIP

# This is the commit message #4:

# Finish


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here the unuseful messages are commented out, and if we have useful ones or want to add more info, we can do it using bullet points below the commit title (in this case &lt;code&gt;Add theming support&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Once finished, push the changes to your Github repository:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;git push --set-upstream origin add-theming&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Create the PR and merge changes&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Now go to the Github repository, you'll se a button to create the PR:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuvkrrqnbylhv9njmd0eh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuvkrrqnbylhv9njmd0eh.png" alt="Create Pull Request"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once it's created, you'll see your changes, the title will be the first line of your commit message, and the body will be the rest of the commit message:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcl1gsqhgkkm5s2tvjocm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcl1gsqhgkkm5s2tvjocm.png" alt="Pull Request"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the "Create Pull Request" button and the PR will be created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgpq0xy3cyqahazr5ugcr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgpq0xy3cyqahazr5ugcr.png" alt="Rebase"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally press the "Rebase and merge" button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's it!&lt;/p&gt;

&lt;p&gt;If you follow this workflow you'll always have a single commit for each PR with the benefits mentioned before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu4svjnzpiwn70gqvcdg8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu4svjnzpiwn70gqvcdg8.png" alt="commits"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to comment on your preferred workflows and the reasons why you like them or you have suggestions or criticisms for this workflow, let me know in the comments.&lt;/p&gt;

</description>
      <category>github</category>
      <category>rebase</category>
      <category>git</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How to setup Semantic Release for a React app or a Next.js app</title>
      <dc:creator>amalv</dc:creator>
      <pubDate>Tue, 28 Apr 2020 21:35:25 +0000</pubDate>
      <link>https://forem.com/amalv/how-to-setup-semantic-release-for-a-react-app-or-a-next-js-app-25c1</link>
      <guid>https://forem.com/amalv/how-to-setup-semantic-release-for-a-react-app-or-a-next-js-app-25c1</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this guide you'll learn how to setup &lt;a href="https://github.com/semantic-release/semantic-release" rel="noopener noreferrer"&gt;Semantic Release&lt;/a&gt; for a &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt; app or a &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt; app. &lt;/p&gt;

&lt;p&gt;This will allow you to follow a workflow that performs fully automated releases for Github and enforces the &lt;a href="https://semver.org/" rel="noopener noreferrer"&gt;Semantic Versioning&lt;/a&gt; specification based on your commit messages.&lt;/p&gt;

&lt;p&gt;Here is an example from the &lt;a href="https://github.com/semantic-release/semantic-release/blob/master/README.md" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; of the release type that will be done based on commit messages:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Commit message&lt;/th&gt;
&lt;th&gt;Release type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;fix(pencil): stop graphite breaking when too much pressure applied&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Patch Release&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;feat(pencil): add 'graphiteWidth' option&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;del&gt;Minor&lt;/del&gt; Feature Release&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;perf(pencil): remove graphiteWidth option&lt;/code&gt;&lt;br&gt;&lt;br&gt;&lt;code&gt;BREAKING CHANGE: The graphiteWidth option has been removed.&lt;/code&gt;&lt;br&gt;&lt;code&gt;The default graphite width of 10mm is always used for performance reasons.&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;del&gt;Major&lt;/del&gt; Breaking Release&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Tools such as &lt;a href="https://github.com/commitizen/cz-cli" rel="noopener noreferrer"&gt;commitizen&lt;/a&gt; or &lt;a href="https://github.com/conventional-changelog/commitlint" rel="noopener noreferrer"&gt;commitlint&lt;/a&gt; can be used to enforce valid commit messages.&lt;/p&gt;

&lt;p&gt;You can use the &lt;a href="https://marketplace.visualstudio.com/items?itemName=KnisterPeter.vscode-commitizen" rel="noopener noreferrer"&gt;commitizen extension&lt;/a&gt; to add commitizen support to Visual Studio Code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic setup
&lt;/h2&gt;

&lt;p&gt;First, create a Next.js app using &lt;a href="https://create-next-app.js.org/" rel="noopener noreferrer"&gt;Create Next App&lt;/a&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx create-next-app semantic-release --example with-typescript --use-npm


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or if you prefer to use only React, use &lt;a href="https://github.com/facebook/create-react-app" rel="noopener noreferrer"&gt;Create React App&lt;/a&gt; and run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx create-react-app semantic-release --template typescript --use-npm


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Create a Github repository
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/new" rel="noopener noreferrer"&gt;https://github.com/new&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example I called it: &lt;code&gt;semantic-release&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Link local repository to Github repository
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

git remote add origin git@github.com:&amp;lt;username&amp;gt;/&amp;lt;repository-name&amp;gt;.git
git push -u origin master


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Github token
&lt;/h2&gt;

&lt;p&gt;A Github token must be created in order for Semantic Release to be able to publish a new release to the Github repository.&lt;/p&gt;

&lt;p&gt;You can read &lt;a href="https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line" rel="noopener noreferrer"&gt;here&lt;/a&gt; how to create a token for Github. You need to give the token repo scope permissions.&lt;/p&gt;

&lt;p&gt;Once you have the token, you have to save it in your repository secrets config:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

https://github.com/&amp;lt;username&amp;gt;/&amp;lt;repositoryname&amp;gt;/settings/secrets


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use &lt;code&gt;GH_TOKEN&lt;/code&gt; as the secret name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Semantic Release Git and Changelog plugins
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install --save-dev @semantic-release/git @semantic-release/changelog


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;These plugins are necessary in order to create a Changelog and publish the new release in Github.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Semantic Release config to package.json
&lt;/h2&gt;

&lt;p&gt;Add the following config to your package.json:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  "private": true,
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    "@semantic-release/changelog",
    "@semantic-release/github",
    "@semantic-release/npm",
    "@semantic-release/git"
  ],
  "release": {
    "prepare": [
      "@semantic-release/changelog",
      "@semantic-release/npm",
      {
        "path": "@semantic-release/git",
        "assets": [
          "package.json",
          "package-lock.json",
          "CHANGELOG.md"
        ],
        "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
      }
    ]
  }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When you set the &lt;code&gt;private&lt;/code&gt; property to true, it skips the publication to the NPM repository, which in this case we don't want to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup Github Actions
&lt;/h2&gt;

&lt;p&gt;Github Actions will be used to create new releases of your app.&lt;/p&gt;

&lt;p&gt;You must store workflows in the &lt;code&gt;.github/workflows&lt;/code&gt; directory in the root of your repository. Once you created the directories, add a &lt;code&gt;main.yml&lt;/code&gt; file inside with the following config:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;name: Semantic release &lt;/p&gt;

&lt;p&gt;on:&lt;br&gt;
  push:&lt;br&gt;
    branches:&lt;br&gt;
      - master&lt;br&gt;
jobs:&lt;br&gt;
  publish:&lt;br&gt;
    runs-on: ubuntu-latest&lt;br&gt;
    steps:&lt;br&gt;
      - uses: actions/checkout@v2&lt;br&gt;
      - name: Setup Node.js&lt;br&gt;
        uses: actions/setup-node@v1&lt;br&gt;
        with:&lt;br&gt;
          node-version: 12&lt;br&gt;
      - name: Install dependencies&lt;br&gt;
        run: npm install &lt;br&gt;
      - name: Build app&lt;br&gt;
        run: npm run build &lt;br&gt;
      - name: Semantic release&lt;br&gt;
        env:&lt;br&gt;
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}&lt;br&gt;
        run: npx semantic-release&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Commit and push changes&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Use the following commands:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;git add .&lt;br&gt;
git commit -m "feat: Add Semantic Release and Github Actions"&lt;br&gt;
git push&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Congratulations! &lt;/p&gt;

&lt;p&gt;If you followed these steps, you should now have triggered Github Actions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzutm6lg2cl26yg2b961k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzutm6lg2cl26yg2b961k.png" alt="Github Actions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, if you check the release tab in your repository, you'll also see your first published release:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb7lno809y3h66shlq9ob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb7lno809y3h66shlq9ob.png" alt="Github Release"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, a Changelog file should have been automatically generated and published:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fiw87456b3mw89i3glk7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fiw87456b3mw89i3glk7z.png" alt="Changelog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can find the complete code for this guide in this &lt;a href="https://github.com/amalv/semantic-release" rel="noopener noreferrer"&gt;github repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>semanticrelease</category>
      <category>nextjs</category>
      <category>react</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Publish Storybook components to NPM using Semantic Release and Github Actions</title>
      <dc:creator>amalv</dc:creator>
      <pubDate>Thu, 12 Mar 2020 16:54:51 +0000</pubDate>
      <link>https://forem.com/amalv/publish-storybook-components-to-npm-using-semantic-release-and-github-actions-6ie</link>
      <guid>https://forem.com/amalv/publish-storybook-components-to-npm-using-semantic-release-and-github-actions-6ie</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this guide you'll learn how to publish your Storybook components to NPM. In this way the components can be easily distributed and then consumed by the client apps.&lt;/p&gt;

&lt;p&gt;Semantic Release will be used in combination with Github Actions in order to automate the release versioning.&lt;/p&gt;

&lt;h4&gt;
  
  
  Basic React Setup
&lt;/h4&gt;

&lt;p&gt;First Create React App must be installed. The following command will generate a Create React App with Typescript support and NPM as the package manager:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app storybook-npm --template typescript --use-npm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that instead of &lt;code&gt;storybook-npm&lt;/code&gt; you'll have to choose your own unique name to publish to NPM or use the &lt;a href="https://docs.npmjs.com/creating-and-publishing-scoped-public-packages"&gt;scoped package&lt;/a&gt; approach.&lt;/p&gt;

&lt;h4&gt;
  
  
  Initialize Storybook
&lt;/h4&gt;

&lt;p&gt;Add Storybook to the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd storybook-npm
npx -p @storybook/cli sb init --story-format=csf-ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check that it works by running the &lt;code&gt;npm run storybook&lt;/code&gt; command.&lt;/p&gt;

&lt;h4&gt;
  
  
  Install and configure Semantic Release
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev semantic-release
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Semantic Release has a perfectly fine out of the box default config, the only thing we need to do here is to add the plugins we want to use in the &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    "@semantic-release/changelog",
    "@semantic-release/github",
    "@semantic-release/npm",
    "@semantic-release/git"
  ],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Clean up files
&lt;/h4&gt;

&lt;p&gt;Since this project is not going to be used as a client, let's clean up a little bit and remove all the unnecessary files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd src
rm -rf stories/*
git rm -rf .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Install styled components
&lt;/h4&gt;

&lt;p&gt;Styled Components is going to be used to style our components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install styled-components @types/styled-components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Add button component
&lt;/h4&gt;

&lt;p&gt;As an exportable component example we are going to create a button.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;src&lt;/code&gt; folder create a new &lt;code&gt;components&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;components&lt;/code&gt; folder add the Button component:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Button.tsx&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";

export interface ButtonProps {
  primary?: boolean
}

export const Button = styled.button&amp;lt;ButtonProps&amp;gt;`
  /* Adapt the colors based on primary prop */
  background: ${props =&amp;gt; props.primary ? "palevioletred" : "white"};
  color: ${props =&amp;gt; props.primary ? "white" : "palevioletred"};

  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still inside the &lt;code&gt;components&lt;/code&gt; folder add an index to export this and future components:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;index.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export * from "./Button";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Add index to &lt;code&gt;src&lt;/code&gt; folder
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;index.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export * from "./components";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will export our components in order to allow clients to consume them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add the Button stories inside the &lt;code&gt;stories&lt;/code&gt; folder
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { action } from '@storybook/addon-actions';
import { Button } from "../components/Button";

export default {
  title: 'Button',
  component: Button,
};

export const Default = () =&amp;gt; &amp;lt;Button onClick={action('clicked')}&amp;gt;Default Button&amp;lt;/Button&amp;gt;;
export const Primary = () =&amp;gt; &amp;lt;Button primary onClick={action('clicked')}&amp;gt;Primary Button&amp;lt;/Button&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Check that the new component is being displayed in Storybook
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run storybook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should now see the &lt;code&gt;Default&lt;/code&gt; and &lt;code&gt;Primary&lt;/code&gt; buttons being displayed in Storybook under the Button story.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JgkJiYKS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tedem44iri43s3dpdk2f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JgkJiYKS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tedem44iri43s3dpdk2f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Create a Github repository
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://github.com/new"&gt;https://github.com/new&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example I called it the same name as the package: &lt;code&gt;storybook-npm&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Link local repository to Github repository
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add origin git@github.com:&amp;lt;username&amp;gt;/&amp;lt;repository-name&amp;gt;.git
git push -u origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Commit and push changes
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "feat: Add button component"
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Github and NPM tokens
&lt;/h4&gt;

&lt;p&gt;We need to get Github and NPM tokens. This is needed in order for Semantic Release to be able to publish a new release for the Github repository and for the NPM registry.&lt;/p&gt;

&lt;p&gt;You can read &lt;a href="https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line"&gt;here&lt;/a&gt; how to create a token for Github. You need to give the token repo scope permissions.&lt;/p&gt;

&lt;p&gt;And &lt;a href="https://docs.npmjs.com/creating-and-viewing-authentication-tokens"&gt;here&lt;/a&gt; you can read how to create a token in NPM. You need to give the token Read and Publish access level.&lt;/p&gt;

&lt;p&gt;Once you have the two tokens, you have to set them in your repository secrets config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://github.com/&amp;lt;username&amp;gt;/&amp;lt;repositoryname&amp;gt;/settings/secrets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;GH_TOKEN&lt;/code&gt; and &lt;code&gt;NPM_TOKEN&lt;/code&gt; as the secret names.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setup Github Actions
&lt;/h4&gt;

&lt;p&gt;Inside the root of the project, create a &lt;code&gt;.github&lt;/code&gt; folder, and inside the &lt;code&gt;.github&lt;/code&gt; folder, add a &lt;code&gt;main.yml&lt;/code&gt; file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Semantic release 

on: push
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 12
      - name: Install dependencies
        run: npm install 
      - name: Build app
        run: npm run build 
      - name: Semantic release
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npx semantic-release

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Commit and push changes
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m 'feat: Add github actions'
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because of the config previously added, the push will trigger Github Actions which runs Semantic Release. You can see the results in your repository action tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VmD1ip-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zy5ieb3ycqehmj9e6b3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VmD1ip-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zy5ieb3ycqehmj9e6b3d.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Github Release
&lt;/h4&gt;

&lt;p&gt;If everything went well, you should see in the action results that every step was succesfully executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mxllSRJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4o7u8dkn75pcx90hhfhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mxllSRJf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4o7u8dkn75pcx90hhfhy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And in the code tab you can see now that a new release has been created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eW7fXyCO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u6nl98bgxhx3adv4mlpl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eW7fXyCO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u6nl98bgxhx3adv4mlpl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, the NPM package has not been published, in order to fix this, a couple of changes need to be made.&lt;/p&gt;

&lt;h4&gt;
  
  
  NPM Release
&lt;/h4&gt;

&lt;p&gt;Update the &lt;code&gt;tsconfig.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "outDir": "dist",
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": false,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll also need to remove the private property from &lt;code&gt;package.json&lt;/code&gt; in order to be able to publish to NPM and add the &lt;code&gt;files&lt;/code&gt; and &lt;code&gt;main&lt;/code&gt; entries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "files": [
    "dist"
  ],
  "main": "dist/index.js",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;file&lt;/code&gt; will indicate to NPM that &lt;code&gt;dist&lt;/code&gt; is the folder to be included when the package is installed as a dependency.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;main&lt;/code&gt; represents the dependency entry point.&lt;/p&gt;

&lt;p&gt;Commit and push changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "Enable NPM registry support"
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should trigger again Github Actions and this time the package will be published to the NPM registry.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use the dependency with a client app
&lt;/h4&gt;

&lt;p&gt;To try the NPM package, we'll create a new Create React App:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app storybook-consumer --use-npm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then install the dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install storybook-npm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit &lt;code&gt;App.js&lt;/code&gt; in order to test it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Button } from 'storybook-npm';
...
&amp;lt;Button&amp;gt;Test&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And start the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should now see the button in the main page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qENYljwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u0vflclalxtaobt7e78z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qENYljwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u0vflclalxtaobt7e78z.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Having a good strategy for releasing your Storybook components can make things easier to maintain. Semantic Release in combination with Github Actions automates the release process so you only have to worry about choosing the appropiate commit messages.&lt;/p&gt;

&lt;p&gt;Tools such as &lt;a href="https://github.com/commitizen/cz-cli"&gt;commitizen&lt;/a&gt; or &lt;a href="https://github.com/conventional-changelog/commitlint"&gt;commitlint&lt;/a&gt; can be used to enforce valid commit messages.&lt;/p&gt;

&lt;p&gt;You can find the complete code for this guide in the &lt;a href="https://github.com/amalv/storybook-npm"&gt;github repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>semanticrelease</category>
      <category>storybook</category>
      <category>githubactions</category>
      <category>react</category>
    </item>
    <item>
      <title>Set up Storybook using Typescript, Material UI and Styled Components</title>
      <dc:creator>amalv</dc:creator>
      <pubDate>Mon, 09 Mar 2020 20:33:40 +0000</pubDate>
      <link>https://forem.com/amalv/set-up-storybook-using-typescript-material-ui-and-styled-components-fem</link>
      <guid>https://forem.com/amalv/set-up-storybook-using-typescript-material-ui-and-styled-components-fem</guid>
      <description>&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;In this tutorial you'll learn how to create a basic foundation for Storybook using Typescript, Material UI and Styled Components.&lt;/p&gt;

&lt;p&gt;MDX will be used as the syntax for Storybook stories.&lt;/p&gt;

&lt;h4&gt;
  
  
  Basic React Setup
&lt;/h4&gt;

&lt;p&gt;First Create React App must be installed. The following command will generate a Create React App with Typescript support:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app storybook --template typescript
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Initialize Storybook
&lt;/h4&gt;

&lt;p&gt;Add Storybook to the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd storybook
npx -p @storybook/cli sb init --story-format=csf-ts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Add and configure Storybook docs
&lt;/h4&gt;

&lt;p&gt;Storybook docs will add support for the Docs tab and the MDX format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D @storybook/addon-docs 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Inside the &lt;code&gt;.storybook&lt;/code&gt; folder you have to update the &lt;code&gt;main.js&lt;/code&gt; file in order to support Storybook docs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  stories: ["../src/stories/**/*.stories.(ts|tsx|js|jsx|mdx)"],
  addons: [
    '@storybook/preset-create-react-app',
    '@storybook/addon-actions',
    '@storybook/addon-links',
    {
      name: "@storybook/addon-docs",
      options: {
        configureJSX: true
      }
    }
  ],
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Add Material UI and Styled Components
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @material-ui/core styled-components
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Check if Storybook runs correctly:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
npm run storybook
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should be able to see the default stories.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add a Button component
&lt;/h4&gt;

&lt;p&gt;As an example, create a &lt;code&gt;components&lt;/code&gt; folder inside &lt;code&gt;src&lt;/code&gt; and add a &lt;code&gt;Button.tsx&lt;/code&gt; file inside:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from 'styled-components';
import Button from '@material-ui/core/Button';

const StyledButton = styled(Button)`
  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border-radius: 3px;
`;

export default StyledButton;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;Button&lt;/code&gt; component from Material UI is being styled using Styled Components.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add Button story
&lt;/h4&gt;

&lt;p&gt;Create a &lt;code&gt;Button.stories.mdx&lt;/code&gt; file inside the &lt;code&gt;stories&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { action } from '@storybook/addon-actions';
import { Meta, Story, Preview, Props } from "@storybook/addon-docs/blocks";
import Button from "../components/Button";

&amp;lt;Meta title="Material Button" /&amp;gt;

Here's some _markdown_!

# Preview

&amp;lt;Preview&amp;gt;
  &amp;lt;Story name="Default"&amp;gt;
    &amp;lt;Button variant="contained" onClick={action("Default button clicked")}&amp;gt;Default&amp;lt;/Button&amp;gt;
  &amp;lt;/Story&amp;gt;
  &amp;lt;Story name="Primary"&amp;gt;
    &amp;lt;Button variant="contained" color="primary" onClick={action("Primary button clicked")}&amp;gt;Primary&amp;lt;/Button&amp;gt;
  &amp;lt;/Story&amp;gt;
  &amp;lt;Story name="Secondary"&amp;gt;
    &amp;lt;Button variant="contained" color="secondary" onClick={action("Secondary button clicked")}&amp;gt;Secondary&amp;lt;/Button&amp;gt;
  &amp;lt;/Story&amp;gt;
&amp;lt;/Preview&amp;gt;

# Props

&amp;lt;Props of={Button} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Fix CSS injection order using a decorator.
&lt;/h4&gt;

&lt;p&gt;Styled Components styles will be overrided by Material UI, in order to fix this a decorator must be added. &lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;config.js&lt;/code&gt; file inside &lt;code&gt;.storybook&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { StylesProvider } from "@material-ui/styles";
import { addDecorator, configure } from '@storybook/react';

const StylesDecorator = storyFn =&amp;gt; (
  &amp;lt;StylesProvider injectFirst&amp;gt;
    {storyFn()}
  &amp;lt;/StylesProvider&amp;gt;
);

addDecorator(StylesDecorator);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Congrats! If you followed these steps you should now have a good foundation to start creating your own stories with a solid Storybook initial configuration.&lt;/p&gt;

&lt;p&gt;You can find the complete code in this &lt;a href="https://github.com/amalv/storybook-foundation"&gt;github repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>storybook</category>
      <category>typescript</category>
      <category>materialui</category>
      <category>mdx</category>
    </item>
  </channel>
</rss>
