<?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 Lubos on Medium]]></title>
        <description><![CDATA[Stories by Lubos on Medium]]></description>
        <link>https://medium.com/@mrlubos?source=rss-8b0435bd8e23------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NUh6ZhGvsMjYFx3D6puPxQ.png</url>
            <title>Stories by Lubos on Medium</title>
            <link>https://medium.com/@mrlubos?source=rss-8b0435bd8e23------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 12:24:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mrlubos/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[Add Keyboard Shortcuts to Your Web App.]]></title>
            <link>https://medium.com/hackernoon/add-keyboard-shortcuts-to-your-web-app-ba358016ff05?source=rss-8b0435bd8e23------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba358016ff05</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[keyboard-shortcuts]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Lubos]]></dc:creator>
            <pubDate>Thu, 15 Mar 2018 17:46:28 GMT</pubDate>
            <atom:updated>2018-03-25T12:41:00.335Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8kEJ0TtaePcMjRPJw7c33Q.jpeg" /></figure><h3>How to Add Keyboard Shortcuts to Your Web App.</h3><h4>React and a few lines of code to the rescue!</h4><p>Imagine a scenario. You work for a large corporation called Adob… you work at a startup called Abode developing a photo editing application called PhotoStore. It sounds like an image storing service, but it is not! In fact, this is a web-based alternative to a popular desktop application.</p><p>One day, your product manager announces that since your competitor has keyboard shortcuts, you need them too to make switching easier for their existing users. After all, this should not delay our launch. You will just add <em>document.addEventListener(‘keydown’, callback);</em> and you are done, right?</p><p>Well, not so fast. Why do we need shortcuts? In our case, we want to be able to activate various tools in the toolbar (think Brush or Selection). But even in our simple use case, we can reasonably expect the need to handle the following.</p><ul><li>activate the selected tool (state management)</li><li>highlight the selected tool (CSS styles)</li><li>do everything above only in the editor (shortcuts should not work in marketing pages, guides, etc.)</li></ul><p>Before we write a line of code, it is always good to think about how a feature might evolve in the future. We can prevent a lot of pain down the road by designing our solution in a way that supports this theoretical functionality, even if we decide not to implement it at this time.</p><h3>Why Even Bother?</h3><p><em>”This does not apply to me, I am not working on a photo editing application,”</em> you think.</p><p>While that might be true, users of every application develop habits and adopt certain workflows. And this includes your app. Offering keyboard shortcuts to the most used features makes your users faster, more productive, and as a result, happier.</p><p>Sometimes, your users cannot or do not want to use their mouse, making shortcuts almost a necessary part of your product. In those cases, I would even count them as a part of the overall accessibility strategy. And you <strong>do</strong> care about that, correct? That’s what I thought. So let’s get started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BA37VtgTEzdGIkwWteKWoQ.jpeg" /><figcaption>Meet your new friends, B and M.</figcaption></figure><h3>Prerequisites.</h3><p>What do we need to deliver this delightful experience? For this article, I will use “only” <a href="https://reactjs.org/">React</a> with <a href="https://reactjs.org/docs/introducing-jsx.html">JSX</a>, allowing me to write compact code. I put <em>only</em> in quotes because setting up React can be daunting if you never worked with it before.</p><p>However, if you are adding shortcuts to your application, chances are, you already use some <a href="https://en.wikipedia.org/wiki/Single-page_application">SPA</a> framework. What we need are the <em>onCreate()</em> and <em>onDestroy()</em> lifecycle methods, so if your framework offers an equivalent, this guide will be easy to follow. In React, these methods are called <a href="https://reactjs.org/docs/react-component.html#componentdidmount"><em>componentDidMount()</em></a> and <a href="https://reactjs.org/docs/react-component.html#componentwillunmount"><em>componentWillUnmount()</em></a>.</p><p>Don’t panic if you use a vanilla JavaScript, you can still follow along. We will be focusing on the concepts, not a particular implementation. But you should be able to read <a href="http://exploringjs.com/es6/">ES6</a>, because I am not about that verbose code life.</p><h3>Project Setup.</h3><p>I managed to get ahold of the Abode PhotoStore application and published it to <a href="https://codepen.io/lmenus/pen/QmwXxb">CodePen</a> so you can get started quickly and follow along. (Hopefully, Abode won’t sue us for leaking their product.)</p><p>As you can see, I obtained an early version of their system. They have not made a design hire yet and there are only 2 tools to choose from — Brush and Select. We can also switch between pages. That’s handy, since we will want to ensure our shortcuts work only in the editor. So for our needs, this will be enough.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Flmenus%2Fembed%2Fpreview%2FQmwXxb%3Fheight%3D600%26slug-hash%3DQmwXxb%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Flmenus%2Ffull%2FQmwXxb%2F&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F313526.QmwXxb.small.ea96ad04-8575-4346-ae7d-af1a5f498632.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/63494716cf78a16ba0af9602306a22b1/href">https://medium.com/media/63494716cf78a16ba0af9602306a22b1/href</a></iframe><h3>Concepts.</h3><p>How will this feature work? We will need to create the following.</p><ol><li><strong>Available shortcuts.</strong> This will be a list of shortcuts we can call. They will map to an object containing all application shortcuts.</li><li><strong>Listeners.</strong> We want to ensure shortcuts work only in the editor. Our framework’s lifecycle methods will come handy here.</li><li><strong>Handlers.</strong> Functions processing keyboard events and actual shortcut execution.</li></ol><h3>Implementation.</h3><p>Let’s begin by defining app shortcuts. This will be an object of all shortcuts that can be executed in the app. For now, we will only be able to select the tools from our toolbar. Every shortcut handler will be a function accepting a <a href="https://developer.mozilla.org/docs/Web/API/KeyboardEvent">KeyboardEvent</a> object as a single parameter. It checks if the event matches the trigger pattern before executing its body.</p><pre>const shortcuts = {</pre><pre>  toolBrush(event) {</pre><pre>    if (event.key !== ‘b’) return false;</pre><pre>    useTool(‘brush’);</pre><pre>    return true;</pre><pre>  },</pre><pre>  toolSelect(event) {</pre><pre>    if (event.key !== ‘m’) return false;</pre><pre>    useTool(‘select’);</pre><pre>    return true;</pre><pre>  },</pre><pre>};</pre><p>To satisfy our requirement of being able to execute only certain shortcuts on certain pages, we will have a list of currently registered shortcuts.</p><pre>let registeredShortcuts = [];</pre><p>When user presses a key, we will iterate through this list and see if the key event executes any shortcut, at which point we stop iterating. If we looped through the <em>shortcuts</em> keys instead, it would be much harder to restrict the shortcut execution to a specific page(s).</p><pre>const handleKeyDown = event =&gt; {</pre><pre>  for (let i = 0; i &lt; registeredShortcuts.length; i += 1) {</pre><pre>    const shortcut = registeredShortcuts[i];</pre><pre>    if (shortcuts[shortcut](event)) {</pre><pre>      event.preventDefault();</pre><pre>      break;</pre><pre>    }</pre><pre>  }</pre><pre>};</pre><p>To start listening for key presses, we register a global listener.</p><pre>document.addEventListener(‘keydown’, handleKeyDown);</pre><p>You can now add <em>’toolBrush’</em> and <em>’toolSelect’</em> inside the <em>registeredShortcuts</em> array to see that our shortcuts do indeed get called.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Flmenus%2Fembed%2Fpreview%2FgebVVL%3Fheight%3D600%26slug-hash%3DgebVVL%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Flmenus%2Ffull%2FgebVVL%2F&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F313526.gebVVL.small.1c46433a-315b-4356-b03b-127c703b4ba7.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee86accce553deb276b30f990935912b/href">https://medium.com/media/ee86accce553deb276b30f990935912b/href</a></iframe><p>Now that we’ve verified our shortcuts work, we can clear the array and write a proper <em>&lt;Shortcut /&gt;</em> component that will handle shortcut registration for us. We will wrap our toolbar buttons inside this component.</p><pre>&lt;Shortcut name=”toolBrush”&gt;</pre><pre>  &lt;Button</pre><pre>    label=”Brush”</pre><pre>    onClick={useTool}</pre><pre>    to=”brush” /&gt;</pre><pre>&lt;/Shortcut&gt;</pre><pre>&lt;Shortcut name=”toolSelect”&gt;</pre><pre>  &lt;Button</pre><pre>    label=”Select”</pre><pre>    onClick={useTool}</pre><pre>    to=”select” /&gt;</pre><pre>&lt;/Shortcut&gt;</pre><p>This has a range of advantages. First of all, when we view the code later, it will be obvious which parts of our application this shortcut affects. It will be also easy to search for a particular shortcut inside our project by typing <em>&lt;Shortcut name=”searched_name_here”&gt;</em>.</p><p>By embedding each shortcut inside a render method of your view, you bind its lifecycle length to the parent component. And using the component lifecycle methods to register and deregister shortcuts makes state management a breeze. When the component gets created, we register the shortcut. Conversely, we deregister the same shortcut on destroy.</p><p>I love pure JavaScript as anyone else, but this would be a nightmare to manage. Here is our component.</p><pre>class Shortcut extends React.Component {</pre><pre>  componentDidMount() {</pre><pre>    if (!registeredShortcuts.includes(this.props.name)) {</pre><pre>      registeredShortcuts = [</pre><pre>        …registeredShortcuts,</pre><pre>        this.props.name,</pre><pre>      ];</pre><pre>    }</pre><pre>  }</pre><pre>  componentWillUnmount() {</pre><pre>    registeredShortcuts = [</pre><pre>      …registeredShortcuts.filter(x =&gt; x !== this.props.name),</pre><pre>    ];</pre><pre>  }</pre><pre>  render() {</pre><pre>    return this.props.children || null;</pre><pre>  }</pre><pre>}</pre><p>Now we need to change our toolbar to be present only in the editor view. Let’s also add styling to it and remove the console information. And that’s it!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Flmenus%2Fembed%2Fpreview%2FyKNBJR%3Fheight%3D600%26slug-hash%3DyKNBJR%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Flmenus%2Ffull%2FyKNBJR%2F&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F313526.yKNBJR.small.14c852f3-f2c1-4250-ad22-46a72471ecc4.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/76338f995db012fa6f1785d2e6a3ba03/href">https://medium.com/media/76338f995db012fa6f1785d2e6a3ba03/href</a></iframe><h3>Addendum.</h3><p>Works like a charm, right? We have successfully added a support for keyboard shortcuts to our app. Are we ready to compete with our nemesis now? Totally. The marketing team can start promoting the 💩 out of this new, shiny feature.</p><p>But as an engineering team, our work is not over yet. That is, unless you intend to quit tomorrow and let your successors deal with this code one beautiful day when it breaks. I used a few shortcuts (pun intended) in this article to keep it simple. Continue reading for further discussion.</p><ul><li>Bundle Optimisation</li><li>Cross-Browser Support</li><li>DevTools</li><li>Edge Cases</li><li>Pattern Matching</li><li>Shortcut Selection</li><li>State Management</li><li>Support Multiple Shortcuts</li><li>Testing</li></ul><h4>Bundle Optimisation.</h4><p>There are 2 things to consider. Instead of using multiple string literals with the same value, we could replace them with a single variable. This variable could then get minified during our build process to shave some bits off the final package.</p><p>The second point would be figuring out whether we need to ship this functionality at all? After all, these are called <em>keyboard</em> shortcuts for a reason. You might not want to serve this code to your mobile users without keyboard. However, as of March 2018, there is no reliable API to detect a presence of a keyboard.</p><h4>Cross-Browser Support.</h4><p>This guide uses a modern set of JavaScript features. If you support older browsers, you would need to ensure you transpile your code and use older browser APIs for event detection.</p><h4>DevTools.</h4><p>For your internal debugging tools, you might want to see which shortcuts are currently registered if something goes wrong. This is very simple, you can just dump the contents of <em>registeredShortcuts</em>.</p><h4>Edge Cases.</h4><p>Let’s say one day we add an input field to our application allowing users to search layers. Now if they want to search for the “background” layer, they can start typing <em>b</em>, but oh là là, the brush tool gets selected. That’s not the worst that could happen, we could be toggling some window or dialog, but it illustrates the issue.</p><p>For this kind of bugs, you will want to check the <em>event.target</em> to ensure it is not a writeable DOM node.</p><h4>Pattern Matching.</h4><p>Yes, our shortcuts work. But try pressing Ctrl/⌘ + B — the brush tool still gets selected! This is clearly not what we want. So instead of a simple check for <em>event.key</em>, we would need to add a precise pattern matching function.</p><h4>Shortcut Selection.</h4><p><a href="https://twitter.com/sashika">Sasha Maximova</a> wrote a <a href="https://medium.com/@sashika/j-k-or-how-to-choose-keyboard-shortcuts-for-web-applications-a7c3b7b408ee">good article</a> on selecting shortcuts for your application so I won’t regurgitate her points. Personally, I prefer to use one-key shortcuts and Shift for two-key combinations as it dramatically decreases chances of clashing with any browser/OS commands. Exception are formatting functions like <strong>bold</strong> where there is no reason to use anything but Ctrl/⌘ + B since your users will be used to that.</p><h4>State Management.</h4><p>I have restrained from throwing a whole state management library like <a href="https://redux.js.org/">Redux</a> into our tutorial to keep it short and simple, but you would probably want to iterate on managing a state in your app. We have inadvertently created a few global variables that would be easily contained with tools like Redux.</p><h4>Support Multiple Shortcuts.</h4><p>Right now, our function accepts only a single string parameter, but there might be a case where you want to register multiple shortcuts at once. For example, if you prefer to declare all shortcuts on top of your view.</p><h4>Testing.</h4><p>Do not ship code without tests. It will be much easier to develop on top of the existing functionality with tests as well as catching potential bugs. If you use an SPA framework (I know you do), you will also want to document your components so it’s easy to see which arguments they support.</p><h3>Conclusion.</h3><p>Adding keyboard shortcuts to your application is a quick win that can improve your user experience without costing you an arm and a leg. Carefully select the most used features and delight your users by enabling them to speed up their workflow.</p><p>Thank you for reading.</p><p>Thank you to friends at <a href="https://scrimba.com/">Scrimba</a> for reading the draft of this article. Visit them for the easiest way to learn code. You can read more from me on my <a href="https://lmen.us/">website</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba358016ff05" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/add-keyboard-shortcuts-to-your-web-app-ba358016ff05">Add Keyboard Shortcuts to Your Web App.</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lessons Learnt from a Repair Store.]]></title>
            <link>https://medium.com/@mrlubos/lessons-learnt-from-a-repair-store-19d5fd10ebfd?source=rss-8b0435bd8e23------2</link>
            <guid isPermaLink="false">https://medium.com/p/19d5fd10ebfd</guid>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[life-lessons]]></category>
            <category><![CDATA[business]]></category>
            <category><![CDATA[mentorship]]></category>
            <category><![CDATA[entrepreneurship]]></category>
            <dc:creator><![CDATA[Lubos]]></dc:creator>
            <pubDate>Mon, 03 Apr 2017 11:30:51 GMT</pubDate>
            <atom:updated>2017-04-03T11:30:51.792Z</atom:updated>
            <content:encoded><![CDATA[<p>I love learning new things serendipitously. This morning, I stumbled upon a blog about starting a phone repair business. As a person aspiring to open a retail store in the future, I was having a hard time resisting the temptation to read this blog. In the end I did not resist, so here are the lessons learnt from a phone repair business.</p><blockquote><strong>Do What You Love Doing.</strong></blockquote><p>You need to love what you do. Starting a business is a long process and it takes even longer to establish it and eventually exit. In addition to that, you will run into many obstacles on your way, making the whole experience even more gruesome. If you do not love your business, what will prevent you from quitting at those times?</p><p>The amount of money, time, and energy you will sink into it is hard to quantify, and it might still not work out in the end. The blog has been up for almost a year and the store is still not open. Looking back, do you want to be able to say you enjoyed building your business or will you feel like you wasted your time?</p><blockquote><strong>Being Honest Pays Off.</strong></blockquote><p>It was an absolute pleasure reading about the process behind building a repair store. This was not because I am super excited about phone repairs, but the author made the story so simple to follow and obfuscated only the most sensitive data.</p><p>I have spoken to many people in the past who would budge at the first inquiry about their business operations. Not surprisingly, none of them held unique patent rights or run a Fortune 500 company. If there are already thousands of new businesses spawning every day in your industry, why worry about helping create another one? At least you could have a new ally in such competitive environment.</p><blockquote><strong>Being Honest Does Not Pay Off.</strong></blockquote><p>As always, apply every advice with caution. It is fine to talk about your headache with coworkers, but think twice before bringing up the visual appearance of your private parts (lesson learnt in the past).</p><p>The same applies in here. The author mentions parking his car a block away before going to see the place he was interested in leasing. When people see you jumping out of a fancy car, they make assumptions. We cannot help it, that’s the way we are programmed.</p><p>The author also paid special attention not to come across as too interested in the property. Otherwise, the owner would have the upper hand in the negotiation. If you choose to be absolutely honest, you should be prepared to face the consequences.</p><blockquote><strong>Hardware Is Expensive.</strong></blockquote><p>It is so cheap to start a software business, one can start thinking every business is just as cheap to start. The reality is that other industries have smaller margins and higher costs. Reading about the phone repair store was a good reminder of the work required to open a real-world equivalent of a landing page. So far, the author has spent about $15,000 and the store is not even open yet.</p><blockquote><strong>You Need a Team.</strong></blockquote><p>That being said, you need an amazing team to push through the obstacles on your way. In this case, they are the people who supply you with products, outfit the store, do the maintenance, and work in the store. There are also accountants, lawyers, and other people you need in almost every business. Pay them well and pay them on time.</p><h3>Conclusion</h3><p>If you are interested, you can read the original blog about <a href="https://howtostartaphonerepairbusiness.com/">starting a phone repair business</a> too. Let me know if you learn any more lessons from it!</p><p>Thank you for reading.</p><p>Originally published at <a href="http://bold.io/@lmenus">bold.io/@lmenus</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19d5fd10ebfd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>