<?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: Fred Hawk</title>
    <description>The latest articles on DEV Community by Fred Hawk (@fredhawk).</description>
    <link>https://dev.to/fredhawk</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%2F86623%2F28770a12-a798-4ae2-9562-e2cc19245e86.jpeg</url>
      <title>DEV Community: Fred Hawk</title>
      <link>https://dev.to/fredhawk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fredhawk"/>
    <language>en</language>
    <item>
      <title>TypeScript beginnings</title>
      <dc:creator>Fred Hawk</dc:creator>
      <pubDate>Sun, 17 May 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/fredhawk/typescript-beginnings-4d7</link>
      <guid>https://dev.to/fredhawk/typescript-beginnings-4d7</guid>
      <description>&lt;h3&gt;
  
  
  Who is this for?
&lt;/h3&gt;

&lt;p&gt;I’m writing this for me and other beginners. Prior knowledge of JavaScript is good if you want to follow along in this post.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is TypeScript?
&lt;/h3&gt;

&lt;p&gt;Typescript is a superset of JavaScript. What does that mean? It means it has all the features of JavaScript and some extras.&lt;/p&gt;

&lt;p&gt;Extras like types and it implements upcoming features that are coming to JavaScript before it arrives. So you can use future features now. It uses the extension .ts. It compiles down to regular JavaScript.&lt;/p&gt;

&lt;p&gt;But the big thing is it adds types to JavaScript which otherwise doesn’t have it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use it?
&lt;/h3&gt;

&lt;p&gt;For me personally it gives me more confidence in my code. The same way testing does but different.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to get started
&lt;/h3&gt;

&lt;p&gt;First you will need to install TypeScript on your computer via the command line.&lt;br&gt;
&lt;/p&gt;

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



&lt;p&gt;Create a typescript file. Something like add.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b) {
  return a + b
}

add(1, 2)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then you would compile the code in the TypeScript file to a JavaScript file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsc add.ts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Run that in the command line to compile it.&lt;/p&gt;

&lt;p&gt;It doesn’t look much different from a regular JavaScript file and it isn’t. We haven’t added types yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are types
&lt;/h3&gt;

&lt;p&gt;Types are a way to describe what kind of parameters you should expect your code to use. These can be simple primitive types or more complex custom types. In the form of interfaces.&lt;/p&gt;

&lt;p&gt;In JavaScript, you also have primitives like strings, numbers and boolean for example. You have the same ones in TypeScript but you can also create your own types in TypeScript.&lt;/p&gt;

&lt;p&gt;For example, you can say that a variable is an object which has certain properties. You then declare what type each property is ahead of time. So the TypeScript compiler can give you and error if you provide a value of the wrong type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add (a: number, b:number): number =&amp;gt; {
  return a + b
}

add(4, 6) // Gives 10
add(4, "red") // Gives an error as it expects both arguments to be numbers.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It checks the input that it matches what the function expects the types to be. If then “b” for example was a string in this case, then the compiler will give an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which types are there?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;li&gt;Array[]&lt;/li&gt;
&lt;li&gt;Function&lt;/li&gt;
&lt;li&gt;Object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Function and object splits into their parts. Such as what the different values are in the parameters to the function and what it returns. Or the properties of the object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Annotation vs inference
&lt;/h3&gt;

&lt;p&gt;You can choose to annotate what type each variable should be or you can let TypeScript infer the type. That means that TypeScript decides the type based on the function you use it in. For example, the add function before. Since the function has to return a number. And it adds up the two values it stands to reason that both has to be numbers. Otherwise, the function cannot return a number.&lt;/p&gt;

&lt;p&gt;It is better to let TypeScript infer as much as possible as it saves time and energy on the programmers part. In such a simple example as above it is better to let the system do the work for you. That way you can put your effort on more complex situations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types from Definitely typed
&lt;/h3&gt;

&lt;p&gt;When you add packages to your project sometimes they don’t come with types. Often you can find types that the community has written for them at a place called &lt;a href="https://definitelytyped.org/"&gt;Definitely typed&lt;/a&gt;. Most of the time you can install the types by doing “npm install @types/packagename”. But sometimes you will have to go check if there are types and the name of the package on the site.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are interfaces?
&lt;/h3&gt;

&lt;p&gt;I compare interfaces to describing the shape of your data. For instance, you have an object with properties. Then you can describe that object through an interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Person = {
  firstName: string,
  lastName: string,
  age: number,
  hometown: {
    lat: number,
    lng: number,
  }
}

const obj = {
  firstName: "Harry",
  lastName: "Potter",
  age: 32,
  hometown: {
    lat: 12.4224,
    lng: 84.2342,
  }
}

function getFullName (personObj: Person) {
  return `${personObj.firstName} ${personObj.lastName}`
}

const fullName = getFullName(obj)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It tells the getFullName function the shape of the personObj that it will receive. I have found that describing data like this helps my mental model of my program. Especially when it gets more complex.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Enums and when to use them?
&lt;/h3&gt;

&lt;p&gt;Enums are a list of constants that you set up ahead of time. If you have a map where the only directions are up, down, left, right. Then you can create an enum of that list called Direction. Then in a function that takes a direction you type one of the inputs as the enum Direction. That means the input can only be one of those four.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Direction {
  Up = "UP",
  Down = "DOWN",
  Left = "LEFT",
  Right = "RIGHT",
}

function move(direction: Direction): void {
  point.direction + 1
}

move(Direction.Up)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is an example of a string enum but there are other versions of it.&lt;/p&gt;

&lt;p&gt;For those of you that have used something like ReduxJS, this is like how you do naming of actions. When using TypeScript you would use an enum instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Classes and TypeScript
&lt;/h3&gt;

&lt;p&gt;Classes in TypeScript are the same as the class syntax in JavaScript. It is like the class based object-oriented programming that many uses in Java or C#. I won’t get into explaining it but for further reading please check out &lt;a href="https://www.typescriptlang.org/docs/handbook/classes.html"&gt;classes in TypeScript.&lt;/a&gt; It explains classes for both JavaScript and TypeScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interfaces vs abstract classes
&lt;/h3&gt;

&lt;p&gt;One part of classes in TypeScript that is not in JavaScript are abstract classes.&lt;/p&gt;

&lt;p&gt;Abstract classes are base classes that other classes can extend from. You cannot instantiate them.&lt;/p&gt;

&lt;p&gt;Interfaces describe what properties and methods you can implement. But doesn’t explain how those methods should work.&lt;/p&gt;

&lt;p&gt;Abstract classes not only describe what you can implement. But also how the methods should work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface InterfaceA {
  method_1(): void
  method_2(): string
}
// vs
abstract class ClassA {
  abstract method_1() // a method with no implementation
  method_2() {
    // a method with implementation
    // do something
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Do you have to use classes?
&lt;/h3&gt;

&lt;p&gt;You can use as much or as little of TypeScripts features as you want. If you do not want to use classes you do not have to.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are generics?
&lt;/h3&gt;

&lt;p&gt;Generics are types that you would use when you do not know the type that the function provides. But for example, you do know that the function will return a value of the same type..&lt;/p&gt;

&lt;p&gt;Example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function general(item: number): number {
  return item
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What if the function should handle cases where it provides a number or a string and you do not know what it will be?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function general(item: number | string): number | string {
  return item
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would work.&lt;/p&gt;

&lt;p&gt;But what about the cases where it needs to be so general that it can accept any type of item?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function general(item: any): any {
  return item
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We should avoid any as much as possible. So how would we solve this? With generics.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function general&amp;lt;T&amp;gt;(item: T): T {
  return item
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The T in this example represent a generic Type. T for type. It doesn’t have to be T, it can be anything but the convention is usually T for Type and K for key.&lt;/p&gt;

&lt;p&gt;What  means is that when we call the function we decide what type it should be.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decorators
&lt;/h3&gt;

&lt;p&gt;Decorators are a more advanced topic than the others and I won’t go deep into it. They are for changing functionality of a class, method, accessor, property or parameter.&lt;/p&gt;

&lt;p&gt;You will need to set the flag “experimentalDecorators” to true in your tsconfig to use them. They are in a proposal in JavaScript as well at the moment. So an experimental feature still.&lt;/p&gt;

&lt;p&gt;They are functions that wrap whatever you are decorating.&lt;/p&gt;

&lt;p&gt;Decorators look like this @expression. You write them on the line above what they decorate.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Decorator function
function bob() {
  console.log("my name is bob")
  return function(target, propertyKey: string, descriptor: PropertyDescriptor) {
    console.log("bob(): called")
  }
}

// Decorating the method
class George {
  @bob()
  method() {
    console.log("my name is George")
  }
}

// Calling it
new George().method()

// Logged in console:
// my name is bob
// bob(): called
// my name is George
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is just one example of how to use decorators. I do not tend to use them, but they give a sense of easy to read about them.&lt;/p&gt;

&lt;p&gt;That is all for now.&lt;/p&gt;

</description>
      <category>typescript</category>
    </item>
    <item>
      <title>Modals, modals, modals!</title>
      <dc:creator>Fred Hawk</dc:creator>
      <pubDate>Sun, 08 Mar 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/fredhawk/modals-modals-modals-5f8n</link>
      <guid>https://dev.to/fredhawk/modals-modals-modals-5f8n</guid>
      <description>&lt;h2&gt;
  
  
  What are modals?
&lt;/h2&gt;

&lt;p&gt;Modals are these seemingly floating things that magically just take over the screen triggered by a certain action. How does that happen? What does it look like on mobile? Is it good for mobile? When is it appropriate to use? Can you animate it?&lt;/p&gt;

&lt;h3&gt;
  
  
  What considerations to think about when making one?
&lt;/h3&gt;

&lt;p&gt;There are a some things to think about when it comes to modals. When are they a good option? They can be used any time you want the users' attention focused on one thing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Showing a lightbox for images&lt;/li&gt;
&lt;li&gt;Display a dialog box confirming that the user really wants to cancel their flight&lt;/li&gt;
&lt;li&gt;Save their changes to a document&lt;/li&gt;
&lt;li&gt;Call to actions to sign up for newsletters and similar&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those are just a few examples but I think you get the point I’m trying to make.&lt;/p&gt;

&lt;p&gt;Other things that I was wondering about when going into this was mobile. How to handle it there? Just use a smaller box or get rid of it or what?&lt;/p&gt;

&lt;p&gt;As it turns out there is a brilliant solution to that which I from now on I will encourage on the &lt;a href="http://w3.org"&gt;w3.org&lt;/a&gt;’s own accessibility site on the subject of dialogs and modals. They suggest that on mobile to turn it into a page that acts like any other. So no box in middle with an overlay but a full site instead. This makes it look less like a modal and more like a new page on mobile which is a nicer experience.&lt;/p&gt;

&lt;p&gt;See the pen below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets build one!
&lt;/h2&gt;

&lt;h3&gt;
  
  
  HTML
&lt;/h3&gt;

&lt;p&gt;What you need is something to trigger the modal to show up. In our case it will be a simple button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;main&amp;gt;
  &amp;lt;button class="modal_show" type="button"&amp;gt;Show Modal&amp;lt;/button&amp;gt;
&amp;lt;/main&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then you need an overlay section that acts like a background to focus the attention on the content.&lt;/p&gt;

&lt;p&gt;And of course you need a dialog box to hold the content. Lastly you will want a way to close the modal in the form of a button. Don’t worry it will not be the only way to close it.&lt;/p&gt;

&lt;p&gt;Let’s add a header and a few inputs so there is some content in the modal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="modal_overlay"&amp;gt;
  &amp;lt;div
    class="modal_content"
    role="dialog"
    aria-labelledby="modal_heading"
    aria-modal="true"
  &amp;gt;
    &amp;lt;h2 id="modal_heading" class="modal_heading"&amp;gt;Modal heading&amp;lt;/h2&amp;gt;
    &amp;lt;div class="modal_form"&amp;gt;
      &amp;lt;div class="modal_form_item"&amp;gt;
        &amp;lt;label for="first_name"&amp;gt;
          &amp;lt;span class="label_text"&amp;gt;First Name:&amp;lt;/span&amp;gt;
        &amp;lt;/label&amp;gt;
        &amp;lt;input
          id="first_name"
          type="text"
          aria-describedby="first_name_desc"
        /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div class="modal_form_item"&amp;gt;
        &amp;lt;label for="last_name"&amp;gt;
          &amp;lt;span class="label_text"&amp;gt;Last Name:&amp;lt;/span&amp;gt;
        &amp;lt;/label&amp;gt;
        &amp;lt;input
          id="last_name"
          type="text"
          aria-describedby="last_name_desc"
        /&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;button type="button" class="modal_close"&amp;gt;Close&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We also want to do is add some attributes to the modal content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;role='dialog'
aria-labelledby="modal_heading"
aria-modal="true"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This adds accessibility to the modal and tells a screen reader that whatever is in the heading is what this modal is about as well as telling the screen reader that it is a modal.Aria-labelledby points to the id of modal_heading and will use that to label it.&lt;/p&gt;

&lt;p&gt;That is all the HTML we need for a modal component.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&gt;

&lt;p&gt;First we get references to the elements we need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const showButton = document.querySelector('.modal_show');
const closeButton = document.querySelector('.modal_close');
const modal = document.querySelector('.modal_overlay');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we need a function that opens and closes the modal when we take certain actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function toggleModal() {
  modal.classList.toggle('active')
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This adds or removes the CSS class active based on what state it is in at the moment.&lt;/p&gt;

&lt;p&gt;Then we need to make listeners for opening and closing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;showButton.addEventListener('click', toggleModal);
closeButton.addEventListener('click', toggleModal)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We also want to be able to close the modal in other ways than the close button remember? For instance through pressing the Escape key.&lt;/p&gt;

&lt;p&gt;So we add another listener.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener('keyup', e =&amp;gt; {
  if (e.keyCode === 27 &amp;amp;&amp;amp; document.querySelector('.modal_overlay.active')) {
    modal.classList.toggle('active');
  }
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This one is tied to the document and not a button that we click.&lt;/p&gt;

&lt;p&gt;Another way we might want to close it is if we click outside the modal. If we click on the overlay so to speak.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener('click', e =&amp;gt; {
  if (e.target === document.querySelector('.modal_overlay.active')) {
    modal.classList.toggle('active');
  }
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We check if where we have clicked is on the overlay. Then we toggle the active class on the modal and since it can only be clicked when the overlay is there it will close the modal.&lt;/p&gt;

&lt;p&gt;Now that we can open and close it we can tie it all together with some CSS to make it work.&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS
&lt;/h3&gt;

&lt;p&gt;First we need to make the overlay look like what it should. We do this by setting the overlay and targeting the role=dialog attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[role='dialog'] {
  background-color: #fff;
  min-height: 100vh;
  padding: 2rem;
}

.modal_overlay {
  position: fixed;
  overflow-y: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.6s ease;
}

@media screen and (min-width: 640px) {
  .modal_overlay {
    background: rgba(0, 0, 0, 0.3);
  }
  [role='dialog'] {
    position: absolute;
    top: 20vh;
    left: 50vw;
    transform: translateX(-50%);
    min-width: calc(640px - (15px * 2));
    min-height: auto;
    box-shadow: 0 19px 38px rgba(0, 0, 0, 0.12), 0 15px 12px rgba(0, 0, 0, 0.22);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The media query is to position and set the size of the box on larger screens so it becomes a box and doesn’t just cover the screen when active.&lt;/p&gt;

&lt;p&gt;On smaller screens it looks weird if there was a small box with dark overlay on an already small screen. Since it should be the focus on the screen it might as well be the only thing on the screen.&lt;/p&gt;

&lt;p&gt;If we want it to show and close when we take certain actions we need to add a class called active that we can toggle.&lt;/p&gt;

&lt;p&gt;The opacity property is for the transition so it fades in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.active {
  visibility: visible;
  opacity: 1;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That is all we really need for it to work, but we are going to add some other base styles simply for visual purposes. To make it look better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

main {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

button {
  background: #63b3ed;
  border: none;
  border-radius: 5px;
  padding: 0.5rem 1rem;
  font-size: 1rem;
}

button:focus {
  background: #2b6cb0;
  color: white;
}

h2 {
  margin-bottom: 1.5rem;
}

input {
  display: block;
  width: 100%;
  margin-bottom: 1.5rem;
}

.modal_close {
  margin-top: 2rem;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The full code and example can be seen on &lt;a href="https://codepen.io/ozinon/pen/WNvKqdv"&gt;CodePen&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Extras
&lt;/h3&gt;

&lt;p&gt;Can use &lt;a href="https://www.npmjs.com/package/dom-focus-lock"&gt;dom-focus-lock&lt;/a&gt; package to help with “trapping” the focus inside the dialog unless you want to do that manually on your own. You will need to use JavaScript to do that.&lt;/p&gt;

&lt;p&gt;There are also versions of it for &lt;a href="https://github.com/theKashey/vue-focus-lock"&gt;Vue&lt;/a&gt; and &lt;a href="https://github.com/theKashey/react-focus-lock"&gt;ReactJS&lt;/a&gt; at the bottom of the NPM link.&lt;/p&gt;

&lt;p&gt;What I learned from this is that there is more to it than just a box showing up magically. Especially if we want this to be accessible to as many people as possible.&lt;/p&gt;

&lt;p&gt;And there you have it. I hope you enjoyed it.&lt;/p&gt;

&lt;p&gt;If there is anything you think that is missing and that should be added please let me know, so I can update it.&lt;/p&gt;

&lt;p&gt;Good resources on this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3.org/TR/wai-aria-practices/examples/dialog-modal/dialog.html"&gt;W3.org Dialog section&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>javascript</category>
      <category>a11y</category>
    </item>
    <item>
      <title>Responsive grid with images or cards</title>
      <dc:creator>Fred Hawk</dc:creator>
      <pubDate>Mon, 24 Feb 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/fredhawk/responsive-grid-with-images-or-cards-256b</link>
      <guid>https://dev.to/fredhawk/responsive-grid-with-images-or-cards-256b</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KnbOceIp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://fredhawk.com/5b8c6e95234db694eaa1aecc5f6025ab/cssgrid_example.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KnbOceIp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://fredhawk.com/5b8c6e95234db694eaa1aecc5f6025ab/cssgrid_example.gif" alt="CSS Grid Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The magic to make it act that way is the two css lines below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;display: grid;
grid-template-columns: repeat(auto-fill, minmax(20rem, 1fr));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Put this on the parent container and this will give you a responsive grid that adapts the columns with the size of the container.&lt;/p&gt;

&lt;p&gt;Here is the whole code example to check out.&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
    </item>
  </channel>
</rss>
