<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by michu2k on Medium]]></title>
        <description><![CDATA[Stories by michu2k on Medium]]></description>
        <link>https://medium.com/@michu2k?source=rss-394f99e5f06------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UpSKTqUBCeISfVRszUYQ8w.jpeg</url>
            <title>Stories by michu2k on Medium</title>
            <link>https://medium.com/@michu2k?source=rss-394f99e5f06------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 19:25:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@michu2k/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Validating data from browser storage with Zod]]></title>
            <link>https://medium.com/@michu2k/validate-data-in-browser-storage-with-zod-be254f465a40?source=rss-394f99e5f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/be254f465a40</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[zod]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[michu2k]]></dc:creator>
            <pubDate>Mon, 18 Dec 2023 06:02:07 GMT</pubDate>
            <atom:updated>2024-03-16T18:31:09.453Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GOhL57UcuUfK2r2dII1_dQ.png" /></figure><p>Whenever your application uses any data storage mechanism, it is important to check that the retrieved data is always correct. Failure to verify external data can result in various problems, so this is an important step to keep your application secure and working well for users.</p><p>Let me show you how you can verify incoming data by creating an additional checking mechanism using <strong>Zod</strong> — a TypeScript-first schema declaration and validation library.</p><h3>Incorrect format</h3><p>The first problem we need to be aware of is that the storage data may not be in the expected format.</p><p>Let’s assume that your app uses a table component and users can customize the data that appears in the table by creating multiple filters, changing sort, et cetera. Modified table settings will be persisted in the local storage, so whenever a user opens the table view again, the saved settings will be applied.</p><p>However, if the application logic changes during development, there is a risk that the existing data loaded from the storage will not be in the correct format, so loading the previous values may cause unwanted errors.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62d668168e333f484da5e434ccc44a77/href">https://medium.com/media/62d668168e333f484da5e434ccc44a77/href</a></iframe><p>In the example above, the sortBy type has been updated and an array with the specified objects is expected instead of a string. According to this change, the storage values will no longer work because of the incorrect format.</p><h3>Unexpected value</h3><p>A similar problem can occur if the value loaded is not the value expected by the function. This can happen because the user can manipulate the data persisted in the store.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b97fb3f0dadfc4a957f1fb406aca6519/href">https://medium.com/media/b97fb3f0dadfc4a957f1fb406aca6519/href</a></iframe><p>Instead of retrieving an ISO Date from the store, a random string is passed, which may lead to unexpected errors when parsing the date.</p><h3>Creating Zod schema</h3><p>You can protect your code from the mentioned problems by using the Zod. Whenever the input data cannot be trusted, you can use the created schema to parse the data and validate it before use.</p><p>This is how a simple z.object definition to validate data from the storage may look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bee36c7aba88a37353c194d0d2373093/href">https://medium.com/media/bee36c7aba88a37353c194d0d2373093/href</a></iframe><p>Each property in the defined object has a specific type that it accept. Additionally, besides the schema, we can also extract the Typescript type that will be used in other parts of the application.</p><h3>Validate storage data</h3><p>To check the incoming data, we can create a function that loads the data from storage and processes it using the newly created schema.</p><p>The .safeParse function is used for parsing, which gives us either the successfully parsed data or an error instance with details about validation problems. Based on the parsing result, the function then either provides the validated data or a null value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9019f9ab75af6ac706e6ac6d1976733/href">https://medium.com/media/f9019f9ab75af6ac706e6ac6d1976733/href</a></iframe><p>By using this simple parsing method, you can make sure your application is safe from unnecessary or incorrect inputs.</p><h3>Conclusion</h3><p>Whenever your application stores data locally on a user’s computer, you should consider using Zod to validate the retrieved data, as this can prevent unexpected behavior or errors in your application.</p><p>Visit the official documentation if you want to learn more about the Zod library: <a href="https://zod.dev/">https://zod.dev/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be254f465a40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Storybook 7 Integration with Next.js]]></title>
            <link>https://medium.com/@michu2k/storybook-7-integration-with-next-js-eda4b1c1d465?source=rss-394f99e5f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/eda4b1c1d465</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[michu2k]]></dc:creator>
            <pubDate>Sat, 18 Mar 2023 17:04:33 GMT</pubDate>
            <atom:updated>2024-09-28T10:46:34.156Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dxrpd6FKDfFhaajXV2TzHQ.jpeg" /></figure><p>If you are a developer looking to improve the development and testing of your UI components, you may want to consider integrating Storybook 7 with your Next.js project. Storybook 7 has many new features and improvements that make it easier than ever to develop and test UI components.</p><p>Here’s a guide on how to set up Storybook with Next.js and create a component in the new CSF 3 format.</p><h4>Setting up Storybook with Next.js</h4><p>Let’s start with setting up the Next.js environment. The easiest way to do this is by using create-next-app.</p><p>Simply run the npx create-next-app@latest command to create a new TypeScript project and follow the quick and easy installation process.</p><p>To add Storybook to your freshly created Next.js application, <br>run the npx storybook@next init command. Storybook will automatically detect the Next.js project and install and configure all necessary packages. Once Storybook is installed, you can run it by typing npm run storybook command.</p><h4>Storybook files</h4><p>After initializing Storybook, you will see that two directories have been created. If you started the Storybook you may have noticed some components listed in the sidebar, this is because the first directory, <em>stories/ </em>contains sample components and this directory is also set as the main location of your story files.</p><p>The second directory<em> .storybook/</em> is created with the <strong>main.ts </strong>file that contains Storybook configuration and the <strong>preview.ts</strong>, which is responsible for rendering stories with global parameters and decorators.</p><h4>Customize config</h4><p>In the configuration, story paths are defined only for the stories/ folder, so I added a new path ../components/**/*.stories.@(js|jsx|ts|tsx) to load stories from the <em>components/</em> directory and enabled <strong>autodocs</strong> to generate an additional docs page for each story</p><p>I also installed and added to the default addons, the <strong>@storybook/addon-a11y </strong>package, which can be helpful in making your UI components more accessible. Additional extensions can unlock advanced features, so if you want to add more functionalities to your Storybook, check out the official Storybook integrations page.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fb189fc3dcf62c5eccf742e0ae59d1b/href">https://medium.com/media/1fb189fc3dcf62c5eccf742e0ae59d1b/href</a></iframe><p>The great thing about the new version of Storybook is that all the features that Next.js supports <em>(Images, styling, routing, absolute imports, etc.)</em> in Storybook 7 will work without the need for additional configuration or addons.</p><h4>Improve previews</h4><p><em>preview.ts </em>is a file where you can define additional parameters, decorators, and JS mocks for all components, as well as additional toolbar configuration. I extended the default parameters by defining new background variants and adding two custom viewports.</p><p>Here’s what the final configuration looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/452e53522b621e7a1acc881dd9ee0ab1/href">https://medium.com/media/452e53522b621e7a1acc881dd9ee0ab1/href</a></iframe><p>Having additional background definitions is a good idea if your component could be displayed on different backgrounds in your application.</p><p>The situation is similar with viewports. If you need to be sure that your component will be displayed correctly in a specific viewport, then you might consider extending the default viewports with your own definitions.</p><p>The <em>preview.ts</em> file is also the best place to put the contexts or providers that are needed to wrap each story. You can achieve this by defining and exporting the <strong>decorators</strong> object. More about decorators <a href="https://storybook.js.org/docs/7.0/react/writing-stories/decorators">here</a>.</p><h4>Writing a story metadata</h4><p>Writing a story in Storybook 7 is slightly different due to the new <strong>CSF 3</strong> format. Not only is the format easier to write and maintain, but type safety has also been improved. In the examples below I use Typescript 4.9+ so the stories can take advantage of the new <strong>satisfies </strong>operator to get better type checking.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f90a1146c6febb4c5d813b0516426aa2/href">https://medium.com/media/f90a1146c6febb4c5d813b0516426aa2/href</a></iframe><p>The metadata defined for the Avatar component includes a primary component definition and a custom title, <em>Display/Avatars</em>, that renders the Avatar stories in the <em>Display </em>category.</p><p>Additionally, a decorator was added to wrap the story in an additional flex wrapper. The great thing about decorators is that they allow you to wrap the stories in custom markup or context and are not visible when you check the story’s source code in the Storybook playground.</p><h4>Component stories</h4><p>For the Avatar component, I added two stories. The first one displays a single Avatar with the specified <strong>src </strong>property, as this is the only required property for this component, while the second one displays multiple component at the same time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7a3e6881cadbe047ee66a566a77bcf2/href">https://medium.com/media/e7a3e6881cadbe047ee66a566a77bcf2/href</a></iframe><p>It is worth noting, that the second <em>AllSizes </em>story will show the avatar in all possible sizes, and since the flex decorator was added earlier to the metadata, there is no need to add an additional wrapper here, as the UI will be displayed correctly.</p><p>By adding the <strong>satisfies </strong>operator, we can strictly type the component arguments. In this case, if the required <strong>src </strong>property were missing, a TypeScript error would be thrown.</p><p>In addition, if you want to improve stories documentation, you can add <strong>JSDoc </strong>to the component’s properties and Storybook will automatically display them.</p><h4>Conclusion</h4><p>In the article you have seen the basic configuration for Storybook, but you can personalize it with addons, decorators, or create your own theme. It is a very powerful tool and I would say nowadays it is a <strong>must-have tool</strong> if you want to build complex applications.</p><p>If you haven’t tried Storybook yet, I recommend you give it a try, as integration with Next.js has never been easier and UI component isolation can give you and your team a lot of benefits, so it’s really worth adding this tool to your project.</p><p>Here are links to the GitHub repository if you want to see the source code, and to the official Storybook articles if you want to learn more about the new CSF 3 format and improved types.</p><p>Repository: <a href="https://github.com/michu2k/next-storybook-example">https://github.com/michu2k/next-storybook-example</a><br>CSF3: <a href="https://storybook.js.org/blog/storybook-csf3-is-here/">https://storybook.js.org/blog/storybook-csf3-is-here/</a><br>Storybook type safety: <a href="https://storybook.js.org/blog/improved-type-safety-in-storybook-7/">https://storybook.js.org/blog/improved-type-safety-in-storybook-7/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eda4b1c1d465" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Animated modal component with Framer Motion]]></title>
            <link>https://medium.com/@michu2k/animated-modal-component-with-framer-motion-174cde5844f8?source=rss-394f99e5f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/174cde5844f8</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[framer-motion]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[modal]]></category>
            <dc:creator><![CDATA[michu2k]]></dc:creator>
            <pubDate>Mon, 10 Oct 2022 16:36:29 GMT</pubDate>
            <atom:updated>2024-10-17T15:29:18.945Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nBBOAux7VrzzjRKR7qU1Jg.png" /></figure><p>Have you ever wondered how to create a generic, animatable modal in React? In this article, I will show you how you can make this type of component using the Framer Motion library — a library used to create powerful, smooth animations via semantic markup.</p><h4>Introduction</h4><p>The modal component will be written on top of the NEXT.js framework with Typescript types and it will be generic, so you will be able to modify and use it in your app without making significant changes to the component.</p><p>I’m assuming that you already know HTML and CSS and you are familiar with React itself, so I won’t write in detail about each attribute, instead, I will focus on the main parts of the code.</p><h4><strong>Component structure</strong></h4><p>When creating a generic component, it is a good practice to create a separate directory and keep all the files related to the component in one place. Doing this will improve the readability and maintenance of the code, so when you want to add additional files in the future <em>(e.g. storybook, tests, etc.) </em>— it will be much easier with an already defined structure.</p><p>In this case, the structure will be quite simple. All files will be placed inside the <strong>Modal </strong>directory, which will be located in the components directory <em>(../components/Modal)</em>.</p><h4><strong>Basic layout</strong></h4><p>Let’s start by writing a basic layout. The modal code should be wrapped with <strong>AnimatePresence </strong>from the <em>framer-motion</em> package. AnimatePresence<strong> </strong>allows components to animate out when they are removed from the React tree. Using this wrapper, the <em>exit</em> property will be enabled, allowing you to define an animation when the component is unmounted.</p><p>The second component used is <strong>motion.div</strong>, which specifies which SVG or HTML element will be animated. This element can be animated via simple properties such as <em>initial</em>, <em>transition</em>, <em>animate, </em>etc. In the following example, an object with defined animation properties will be passed to the component by the spread operator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34d795be8eb64a6653fb7b8fc7058b7d/href">https://medium.com/media/34d795be8eb64a6653fb7b8fc7058b7d/href</a></iframe><p>As you can see, the additional <strong>isVisible </strong>property will be passed to the component by the props and will be used here to determine when the component should render in the React tree or not.</p><p>Additionally, modal is wrapper in a custom <strong>ClientSidePortal </strong>component, that renders the Modal in React <em>portal.</em></p><h4>Animation</h4><p>The next step is to define the animation for the modal, as well as for the backdrop, both of these definitions will be in the <em>Modal.utils.ts</em> file. Before moving on to the animation definitions, here is an explanation of what each animation property represents:</p><p><strong>transition </strong>— A transition defines how values animate from one state to another.<br><strong>initial </strong>— Initial values of the animated element<br><strong>animate </strong>— Values to which the element will animate<br><strong>exit </strong>— Values to which the element will animate if removed from the tree.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/880ee0ed0c7ac727bbc91b894c422b98/href">https://medium.com/media/880ee0ed0c7ac727bbc91b894c422b98/href</a></iframe><p>The modal will be animated for 200ms with a delay of 100ms using the easeInOut timing function. The animation will change the opacity to 100%, scale the modal to a default value and move slightly on the Y axis. And for the exit animation — only opacity will be animated.</p><p>The backdrop will also be animated for 200ms, but only by changing the opacity value.</p><h4>Improving the basic layout</h4><p>The basic layout and animations are defined, but we need to write a few more things. First, I added styles and ARIA attributes to the layout, and along with that, the modal UI and accessibility have been improved.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7c622837bc8a06dcc47c1c434269cf3/href">https://medium.com/media/b7c622837bc8a06dcc47c1c434269cf3/href</a></iframe><p>In addition, I added two additional hooks, which would be nice to have.</p><p>The first one is <strong>useReducedMotion()</strong> from <em>framer-motion</em> package. A hook that determines whether the user prefers reduced motion. It will return a boolean value whether the user has reduced the system animations or not.</p><p>The second hook is <strong>useId()</strong>. A hook introduced in React 18 that is used to generate a unique id for HTML elements. In our case, the value from the hook will be assigned to the <strong>headingId </strong>variable used as the <em>heading id</em> and <em>aria-labelledby</em> value.</p><p>Next, the modal <strong>className </strong>is a combination of the base class and a class provided by the props and merged using a method from the <em>classname </em>package that allows you to combine multiple classes.</p><p>Finally, I added an event listener with <em>keydow</em>n and a function that will invoke when the Escape key is pressed.</p><h4>Content</h4><p>The modal content is not complicated, as it contains only a few elements. The added `button` with a cross icon will close the modal when clicked. In this case, the SVG icon is displayed by the <strong>Image </strong>component from Next.js. What’s more, added span with the sr-only class is used to hide text that is intended only for screen readers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80cfc18266de044537bf5a8103f2ac62/href">https://medium.com/media/80cfc18266de044537bf5a8103f2ac62/href</a></iframe><p>Further code fragments display the modal header with a specified heading and modal content. The content will contain children, so anything wrapped in Modal will be in this section.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80ee32d62ac8974f91d95e461ff300a6/href">https://medium.com/media/80ee32d62ac8974f91d95e461ff300a6/href</a></iframe><h4><strong>Component props</strong></h4><p>Because the visibility state of the modal will be managed outside the component, we need to pass <strong>isVisible </strong>and <strong>onClickCloseBtn </strong>props. The modal heading also appears as a required property. While the other definitions are optional, they will make the component usable in many different cases, when, for example, a different modal size or animations are required.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99c5687cac23d60003a5cd335c15973c/href">https://medium.com/media/99c5687cac23d60003a5cd335c15973c/href</a></iframe><p>I wrote a short comment for each type, so it will be easier to understand what each property is used for.</p><h4><strong>Styles</strong></h4><p>Modal styles can be written in many ways, so in this section, I will not go into the details of all the styles. Instead, I will write a few points that I followed in writing this component.</p><ul><li>The modal, as well as the backdrop, should have position: fixed and z-index values defined.</li><li>The background should cover the entire screen, so the attribute “inset” is set with a value of 0. <em>(The </em><strong><em>inset</em></strong><em> CSS property is a shorthand that corresponds to the top, right, bottom, and left properties.)</em></li><li>The modal will be displayed in the center of the screen, and the button to close it will be visible in the top-right corner.</li><li>Transition for each property will be disabled for the reduced-motion preference.</li></ul><h4><strong>Usage</strong></h4><p>The modal will appear on the screen when the button is clicked. To achieve this, an additional state is created that will be used to manage the visibility of the modal, as well as the <strong>showModal </strong>and <strong>hideModal </strong>functions.</p><p>What remains to be done is to add the previously mentioned button used to open the modal, define the heading, and pass content (as children of the component) to be displayed in the modal.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bde66338238a5022b1f732881033319/href">https://medium.com/media/6bde66338238a5022b1f732881033319/href</a></iframe><h4>Conclusion</h4><p>As you can see, creating an animated modal in React is not that difficult. In most cases, you don’t need to use a third-party library to achieve such an effect.</p><p>Below is a link to the GitHub repository, where you can check out the full source code of the component with additional comments added:</p><p><a href="https://github.com/michu2k/react-framer-motion-modal-example">https://github.com/michu2k/react-framer-motion-modal-example</a></p><p>This was my first article and I hope you like it =).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=174cde5844f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>