<?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 Samuel Kwesi Obuobi on Medium]]></title>
        <description><![CDATA[Stories by Samuel Kwesi Obuobi on Medium]]></description>
        <link>https://medium.com/@kwesiobuobi7?source=rss-4a99be4e7a7f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*B5NU9GbprCsKvB7mz4465w.jpeg</url>
            <title>Stories by Samuel Kwesi Obuobi on Medium</title>
            <link>https://medium.com/@kwesiobuobi7?source=rss-4a99be4e7a7f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 15:13:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kwesiobuobi7/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[Building Elegant Modals]]></title>
            <link>https://medium.com/@kwesiobuobi7/building-elegant-modals-1df0b64514e2?source=rss-4a99be4e7a7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1df0b64514e2</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dialog]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[modal]]></category>
            <dc:creator><![CDATA[Samuel Kwesi Obuobi]]></dc:creator>
            <pubDate>Sat, 13 May 2023 14:37:12 GMT</pubDate>
            <atom:updated>2023-05-13T14:38:06.674Z</atom:updated>
            <content:encoded><![CDATA[<h4>Introducing the &lt;dialog&gt; element</h4><p>In the early days of web development, creating modals or pop-up dialogs required significant effort and often involved complex implementations. Developers sometimes had to rely on various JavaScript libraries or custom coding to achieve the desired modal functionality. Empty &lt;div&gt;elements sometimes ended up in the DOM tree just because we needed an overlay for our modal(s). All of this may or may not have been a dealbreaker to some developers but, what if there was an elegant way to go about this?</p><h4>The &lt;dialog&gt; element</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KnftDedTAfBQbxEK-fdalg.jpeg" /></figure><p>In today’s web development landscape, creating modals or pop-up dialogs has never been easier and more enjoyable, thanks to the introduction of the &lt;dialog&gt; element in HTML. The &lt;dialog&gt; element provides a native and standardized way to design and implement modals, making it a powerful tool for enhancing user experiences on websites. With a few lines of HTML, CSS, and JavaScript, developers can create elegant and interactive modals that engage users and provide a seamless browsing experience. Let’s explore how the &lt;dialog&gt; element makes creating modals in modern web development easy and enjoyable.</p><h4>How is it used?</h4><p>To create a basic modal using the &lt;dialog&gt; element, we can start with a simple HTML structure. The &lt;dialog&gt; element itself acts as the container for our modal content. We can add a button or a link to trigger the opening of the dialog. With some CSS styling, we can customize the appearance of the dialog box, such as setting the width, height, background color, and positioning. Adding JavaScript functionality, we can listen for the button click event and use either of two available methods to display the dialog when triggered. Let’s see those methods.</p><h4>show( ) vs showModal( )</h4><p>Dialogs are hidden by default. They can be shown with the show() method or the showModal() method. When they are shown, they can be hidden with the close() method.</p><ul><li><strong><em>show()</em></strong>: When the .show() method is called on a &lt;dialog&gt; element, it displays the modal as a floating overlay on the page. However, it does not prevent interaction with the rest of the page. This means that users can still interact with elements outside the modal while it is visible.</li><li><strong><em>showModal()</em></strong>: The .showModal() method, on the other hand, displays the modal as a modal dialog box and enforces a modal behavior. When the modal is shown using .showModal(), it creates a blocking effect, preventing interaction with elements outside the modal. Users must interact with the modal or close it before they can interact with the rest of the page. This behavior is commonly desired for true modal experiences where the user’s attention should be focused on the modal content and nothing else.</li></ul><h4>Demo</h4><p>Let’s take a look at a practical example of a modal implemented using the &lt;dialog&gt; element. Below is a snippet of code showcasing a minimalistic modal with HTML and JavaScript:</p><pre>&lt;h1&gt;Simple Modals&lt;/h1&gt;<br><br>&lt;!-- defines the modal content. It is hidden by defalut --&gt;<br>&lt;dialog id=&quot;my-dialog&quot;&gt;<br>  &lt;p&gt;This is the content of the modal.&lt;/p&gt;<br>  &lt;button id=&quot;close-btn&quot;&gt;Close&lt;/button&gt;<br>&lt;/dialog&gt;<br><br>&lt;!-- We&#39;ll add an event listener on this button, to show the modal above --&gt;<br>&lt;button id=&quot;open-btn&quot;&gt;Open Modal&lt;/button&gt;<br><br>&lt;script&gt;<br>  const openBtn = document.getElementById(&#39;open-btn&#39;);<br>  const closeBtn = document.getElementById(&#39;close-btn&#39;);<br>  const dialog = document.getElementById(&#39;my-dialog&#39;);<br><br>  // shows the modal<br>  openBtn.addEventListener(&#39;click&#39;, () =&gt; {<br>    dialog.showModal();<br>  });<br><br>  // closes the modal<br>  closeBtn.addEventListener(&#39;click&#39;, () =&gt; {<br>    dialog.close();<br>  });<br>&lt;/script&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*ijppIqtcK0ZVDK9WcWIQ2A.gif" /></figure><h4>Outro</h4><p>With this simple implementation using the &lt;dialog&gt; element, we’re now able to create modals with ease, saving significant time and effort. The &lt;dialog&gt; element provides a straightforward and intuitive approach to creating modals, enabling developers to deliver engaging and interactive experiences on their websites. No need to deal with the complexities of custom modal implementations.</p><p>Let’s go build those elegant modals!</p><h4>Next Steps</h4><p>Next, we’ll see how to style the dialog or modal box itself, as well as the overlay (backdrop) it creates by default.<br>Till next time…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1df0b64514e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>