<?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>DEV Community: Kaartik Nayak</title>
    <description>The latest articles on DEV Community by Kaartik Nayak (@kaartikn).</description>
    <link>https://dev.to/kaartikn</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%2F956563%2F0bd0a5cd-d3ce-430f-ace2-6e4d68c98b22.png</url>
      <title>DEV Community: Kaartik Nayak</title>
      <link>https://dev.to/kaartikn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kaartikn"/>
    <language>en</language>
    <item>
      <title>MDX Code Highlighting and Styling with Tailwind</title>
      <dc:creator>Kaartik Nayak</dc:creator>
      <pubDate>Mon, 14 Nov 2022 09:30:15 +0000</pubDate>
      <link>https://dev.to/kaartikn/mdx-code-highlighting-and-styling-with-tailwind-el4</link>
      <guid>https://dev.to/kaartikn/mdx-code-highlighting-and-styling-with-tailwind-el4</guid>
      <description>&lt;p&gt;In this blog we will see how to style the code blocks in MDX files using rehype and remake plugins.&lt;/p&gt;

&lt;h2&gt;
  
  
  End Result Of This Blog
&lt;/h2&gt;

&lt;p&gt;Before we start, let's see how the code blocks will look like in MDX files:&lt;/p&gt;

&lt;h2&gt;
  
  
  Code blocks with the styles 🤩
&lt;/h2&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%2Fuploads%2Farticles%2Fbfp2pbewut42397gcme9.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%2Fuploads%2Farticles%2Fbfp2pbewut42397gcme9.png" alt="Code Blocks without the plugins"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;View them at : &lt;a href="https://kaartiknayak.vercel.app/blog/mdx-syntax-styling" rel="noopener noreferrer"&gt;https://kaartiknayak.vercel.app/blog/mdx-syntax-styling&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So lets start with adding the styling to the code blocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Dependencies to the project
&lt;/h2&gt;

&lt;p&gt;Required Dependencies:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rehype&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rehype-autolink-headings&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rehype-code-titles&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rehype-prism-plus&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rehype-slug&lt;/code&gt;&lt;br&gt;
&lt;code&gt;remark-gfm&lt;/code&gt;&lt;/p&gt;

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

npm &lt;span class="nb"&gt;install &lt;/span&gt;rehype rehype-autolink-headings rehype-code-titles rehype-prism-plus rehype-slug remark-gfm
yarn add rehype rehype-autolink-headings rehype-code-titles rehype-prism-plus rehype-slug remark-gfm


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;I am using &lt;code&gt;MDX-Remote&lt;/code&gt; for MDX files. If you are using &lt;code&gt;@next/mdx&lt;/code&gt; then you can use the same plugins in &lt;code&gt;next.config.js&lt;/code&gt; file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Adding plugins to our MDX object
&lt;/h2&gt;

&lt;p&gt;Now let's see how to use these plugins in &lt;code&gt;MDX-Remote&lt;/code&gt;:&lt;/p&gt;

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

import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote } from 'next-mdx-remote'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeCodeTitles from 'rehype-code-titles'
import rehypePrism from '@mapbox/rehype-prism'
import rehypeSlug from 'rehype-slug'
import remarkGfm from 'remark-gfm'

const components = { /* your components */ }

export default function Blog({ source }) {
  return (
    &amp;lt;MDXRemote {...source} components={components} /&amp;gt;
  )
}

export async function getStaticProps({params}) {
  const { fileContents } = getBlogBySlug(params.slug); // get the MDX file contents
  const { data, content } = matter(fileContents); // get the frontmatter, content
  const mdxSource = await serialize(source, {
    mdxOptions: {
      remarkPlugins: [remarkGfm],
      rehypePlugins: [
        rehypeSlug,
        rehypeCodeTitles,
        rehypePrism,
        [
          rehypeAutolinkHeadings,
          {
            properties: {
              className: ["anchor"],
            },
          },
        ],
      ],
      format: "mdx",
    },
  })

  return { props: { source: mdxSource } }
}


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

&lt;/div&gt;

&lt;p&gt;Add the &lt;code&gt;mdxOptions&lt;/code&gt; object in the &lt;code&gt;serialize&lt;/code&gt; function. This will add the plugins to the MDX files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Customizable Styles
&lt;/h2&gt;

&lt;p&gt;Now go to &lt;code&gt;globals.css&lt;/code&gt; file and add the following code so that the code blocks have syntax highlighting&lt;/p&gt;


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

&lt;p&gt;// globals.css&lt;br&gt;
.rehype-code-title {&lt;br&gt;
  @apply px-5 py-3 font-mono text-sm font-bold text-neutral-800 bg-neutral-100 border border-b-0 border-neutral-300 rounded-t-lg dark:text-neutral-200 dark:border-neutral-700 dark:bg-neutral-900;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.rehype-code-title + pre {&lt;br&gt;
  @apply mt-0 rounded-t-none;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.prose .anchor {&lt;br&gt;
  @apply absolute invisible no-underline delay-[50ms] transition-all -ml-10 pr-2 cursor-pointer;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.anchor:hover {&lt;br&gt;
  @apply visible;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.prose a {&lt;br&gt;
  @apply transition-all;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.prose .anchor:after {&lt;br&gt;
  @apply text-neutral-300 dark:text-neutral-700;&lt;br&gt;
  content: "#";&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.prose *:hover &amp;gt; .anchor {&lt;br&gt;
  @apply visible;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.prose pre {&lt;br&gt;
  @apply border shadow-md border-neutral-300 bg-neutral-200 dark:border-neutral-700 dark:bg-neutral-800;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.prose code {&lt;br&gt;
  @apply font-mono text-neutral-800 dark:text-neutral-200 px-1 py-0.5 border border-neutral-400 dark:border-neutral-800 rounded-lg bg-neutral-300 dark:bg-neutral-800;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.prose pre code {&lt;br&gt;
  @apply text-neutral-800 dark:text-neutral-200 p-0;&lt;br&gt;
  border: initial;&lt;br&gt;
}&lt;br&gt;
.code-line {&lt;br&gt;
  @apply bg-neutral-200 dark:bg-neutral-800;&lt;br&gt;
}&lt;br&gt;
.token.comment,&lt;br&gt;
.token.prolog,&lt;br&gt;
.token.doctype,&lt;br&gt;
.token.cdata,&lt;br&gt;
.token.console {&lt;br&gt;
  @apply text-neutral-700 dark:text-neutral-300;&lt;br&gt;
}&lt;br&gt;
.token.punctuation {&lt;br&gt;
  @apply text-blue-400;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.token.number,&lt;br&gt;
.token.symbol,&lt;br&gt;
.token.boolean,&lt;br&gt;
.token.constant,&lt;br&gt;
.token.property,&lt;br&gt;
.token.tag,&lt;br&gt;
.token.deleted {&lt;br&gt;
  @apply text-blue-500;&lt;br&gt;
}&lt;br&gt;
.token.string {&lt;br&gt;
  @apply text-blue-600;&lt;br&gt;
}&lt;br&gt;
.token.attr-name,&lt;br&gt;
.token.builtin,&lt;br&gt;
.token.char,&lt;br&gt;
.token.selector,&lt;br&gt;
.token.inserted {&lt;br&gt;
  @apply text-purple-500;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.token.entity,&lt;br&gt;
.token.important,&lt;br&gt;
.token.operator,&lt;br&gt;
.token.regex,&lt;br&gt;
.token.url,&lt;br&gt;
.token.variable,&lt;br&gt;
.language-css .token.string,&lt;br&gt;
.style .token.string {&lt;br&gt;
  @apply text-yellow-900 dark:text-yellow-300;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.token.atrule,&lt;br&gt;
.token.attr-value,&lt;br&gt;
.token.keyword {&lt;br&gt;
  @apply text-red-600;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.token.function,&lt;br&gt;
.token.class-name {&lt;br&gt;
  @apply text-purple-500;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;code[class*="language-"],&lt;br&gt;
pre[class*="language-"] {&lt;br&gt;
  @apply text-neutral-800 dark:text-neutral-200;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;blockquote p {&lt;br&gt;
  @apply font-mono;&lt;br&gt;
}&lt;br&gt;
blockquote p::before {&lt;br&gt;
  content: "" !important;&lt;br&gt;
}&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;In this article, we learned how to add syntax highlighting to code blocks in Next.js using MDX and TailwindCSS.&lt;br&gt;
All the styles can be customized as per your need. You can also add more plugins to the &lt;code&gt;mdxOptions&lt;/code&gt; object to add more features to your MDX files.&lt;/p&gt;

&lt;p&gt;Resources&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/hashicorp/next-mdx-remote" rel="noopener noreferrer"&gt;MDX-Remote&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nextjs.org/docs/advanced-features/using-mdx" rel="noopener noreferrer"&gt;Next-MDX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mdxjs.com" rel="noopener noreferrer"&gt;MDX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nextjs.org" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tailwindcss.com" rel="noopener noreferrer"&gt;TailwindCSS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Plugins&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/rehypejs/rehype-autolink-headings" rel="noopener noreferrer"&gt;rehype-autolink-headings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/rehype-code-titles" rel="noopener noreferrer"&gt;rehype-code-titles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/rehype-prism-plus" rel="noopener noreferrer"&gt;rehype-prism-plus&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/rehype-slug" rel="noopener noreferrer"&gt;rehype-slug&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/remark-gfm" rel="noopener noreferrer"&gt;remark-gfm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's a wrap! If you like this blog, please like and share this blog.&lt;br&gt;
Also, do follow me on &lt;a href="https://twitter.com/KaartikNayak" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;. Peace Out! ✌️&lt;/p&gt;

</description>
      <category>mdx</category>
      <category>syntaxhighlighting</category>
      <category>nextjs</category>
      <category>tailwindcss</category>
    </item>
    <item>
      <title>Using UseRef hook for form data</title>
      <dc:creator>Kaartik Nayak</dc:creator>
      <pubDate>Thu, 27 Oct 2022 02:38:41 +0000</pubDate>
      <link>https://dev.to/kaartikn/using-useref-hook-for-form-data-2el5</link>
      <guid>https://dev.to/kaartikn/using-useref-hook-for-form-data-2el5</guid>
      <description>&lt;p&gt;In this blog we will see how to use useRef hook instead of the useState hook.&lt;/p&gt;

&lt;p&gt;The usual way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function Form() {
  const [email, setEmail] = React.useState("");
  const [password, setPassword] = React.useState("");

  const submit = (e) =&amp;gt; {
    e.preventDefault();
    console.log(email, password);
  };
  // rerenders component eveytime when a character is added in the input field
  React.useEffect(() =&amp;gt; {
    console.log("value changed");
  }, [email, password]);

  return (
    &amp;lt;form onSubmit={submit}&amp;gt;
      &amp;lt;label htmlFor="email"&amp;gt;Email&amp;lt;/label&amp;gt;
      &amp;lt;input
        type="email"
        name="email"
        value={email}
        onChange={(e) =&amp;gt; setEmail(e.target.value)}
      /&amp;gt;
      &amp;lt;label htmlFor="password"&amp;gt;Password&amp;lt;/label&amp;gt;
      &amp;lt;input
        type="password"
        name="password"
        value={password}
        onChange={(e) =&amp;gt; setPassword(e.target.value)}
      /&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using &lt;code&gt;useRef&lt;/code&gt; hook
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function Ref() {
  const emailRef = React.useRef(null);
  const passwordRef = React.useRef(null);
  const submit = (e) =&amp;gt; {
    e.preventDefault();
    console.log(emailRef.current.value, passwordRef.current.value);
  };
  return (
    &amp;lt;form onSubmit={submit}&amp;gt;
      &amp;lt;label htmlFor="email"&amp;gt;Email&amp;lt;/label&amp;gt;
      &amp;lt;input ref={emailRef} type="email" name="email" /&amp;gt;
      &amp;lt;label htmlFor="password"&amp;gt;Password&amp;lt;/label&amp;gt;
      &amp;lt;input ref={passwordRef} type="password" name="password" /&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we use &lt;code&gt;useRef&lt;/code&gt;hook, the component does not rerender when the value of the input field changes. This is because the &lt;code&gt;useRef&lt;/code&gt; hook does not cause any rerenders. It is used to store the reference of the DOM element.&lt;/p&gt;

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

&lt;p&gt;In this blog we saw how to use &lt;code&gt;useRef&lt;/code&gt; hook instead of &lt;code&gt;useState&lt;/code&gt; hook. This is useful when we want to store the value of the input field without causing any rerenders 👌&lt;/p&gt;

&lt;p&gt;So that's it for this blog.&lt;br&gt;
Hope you liked it 🤗&lt;br&gt;
View my &lt;a href="https://kaartiknayak.vercel.app"&gt;portfolio app&lt;/a&gt;&lt;br&gt;
Also, do follow me on &lt;a href="https://twitter.com/KaartikNayak"&gt;Twitter&lt;/a&gt; &amp;lt;3&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthooks</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
