<?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: Steven Straatemans</title>
    <description>The latest articles on DEV Community by Steven Straatemans (@sstraatemans).</description>
    <link>https://dev.to/sstraatemans</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%2F269012%2F5089ed55-1170-45b9-9401-1076e864c081.png</url>
      <title>DEV Community: Steven Straatemans</title>
      <link>https://dev.to/sstraatemans</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sstraatemans"/>
    <language>en</language>
    <item>
      <title>VSCode Live Share: Pair programming done right</title>
      <dc:creator>Steven Straatemans</dc:creator>
      <pubDate>Tue, 23 Mar 2021 10:40:43 +0000</pubDate>
      <link>https://dev.to/sstraatemans/vscode-live-share-pair-programming-done-right-10k8</link>
      <guid>https://dev.to/sstraatemans/vscode-live-share-pair-programming-done-right-10k8</guid>
      <description>&lt;p&gt;Two heads are better than one.&lt;br&gt;
That is why pair-programming is a big thing in certain companies. Some teams really thrive on it and get great results.&lt;br&gt;
Me? I'm not a big fan of it. I never got used to sitting with 2 people behind one desk. The driver typing and the navigator trying to explain what they think should be changed. The latter getting more frustrated by the minute with the former. Pointing furiously at the screen where they want the changes in the code. Finally shoving the other person aside and type what they think works. Or just walking away and writing the code behind their own desk.&lt;/p&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%2Ftgeiaqv9h4z5g8cqtxay.jpeg" 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%2Ftgeiaqv9h4z5g8cqtxay.jpeg" alt="Muppets Statler and Waldorf doing the pair programming"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last year, working from home, pair programming was even harder. You had to do it via a screen share. It is harder to make your point when you are not in control and can't point at the screen. &lt;br&gt;
The only upside is that it's easier to scream in silence. Just mute your microphone. Again, this could just be me and my lack of communication skills. Sometimes the preconceptions about developers are real.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;I was today years old when I found out about VSCode Live Share. And it was just shy of a religious experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Today I had an issue getting Typescript to play nicely with some RxJS. I asked a colleague to have a look. So I thought we could do a screen share. I was dreading it a little, but I wasted to much time already with this issue and my colleague is really good in explaining stuff, so why not.&lt;br&gt;
Instead of asking me to share my screen, the default opening line for every pair programming session via Slack, they suggested to use Live Share in Visual Code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is this Live Sharing thing?
&lt;/h2&gt;

&lt;p&gt;If this is your response as well, then keep reading.&lt;/p&gt;

&lt;p&gt;Live share is a plugin from Microsoft, the creators of VSCode as you might know. It enables you to collaboratively edit and debug with others in real time.&lt;/p&gt;

&lt;p&gt;To start it up is really simple.&lt;br&gt;
All I had to do was install Live Share in Visual Studio Code and all was revealed. It took 1 minute. After installation I sent an invite to my colleague with their Microsoft account, share the generated link and that was it.&lt;/p&gt;

&lt;p&gt;A couple of seconds later I saw an extra cursor appear, with the name of my colleague. In my code! And they could do whatever they wanted. They made changes in the code. At the same time I could make changes in the code. In the same file!&lt;/p&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%2Flou3yfqe34h2vjcas6fp.gif" 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%2Flou3yfqe34h2vjcas6fp.gif" alt="Working together in the same editor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It just blew my mind. Of course it could be that I have been living under a rock for the last 3 years and every developer knows about this little gem already. If that is the case, so sorry to waste your time.&lt;/p&gt;

&lt;p&gt;The brilliant thing is, it's not like a screen share whatsoever. My colleague was working in their own VSCode, with their own plugins, their own settings, but within my environment and my code. They could be using a light theme for all I cared (I don't know why one would ever want to burn holes in their retina, but they could!)&lt;/p&gt;

&lt;p&gt;This is the way pair-programming should be. You shouldn't have to explain to someone what they should do. And be annoyed when that person doesn't understand directly what you mean.&lt;br&gt;
Not everybody has the communicative qualities to explain to others what they need to do. I admire those who can.&lt;br&gt;
Now you can just type your code and the other person can react to that.&lt;/p&gt;

&lt;p&gt;It's not just useful for pair-programming. But just think how useful it would be teaching someone a programming concept with this tool.&lt;/p&gt;

&lt;p&gt;And it gets better. Apparently I only used the basic features. But there is so much more. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can share a server where both users can start the app via localhost.&lt;/li&gt;
&lt;li&gt;You can share a terminal.&lt;/li&gt;
&lt;li&gt;You can limit the files your collaborators have access to.&lt;/li&gt;
&lt;li&gt;You can follow an other user when they are trying to explain something. So that your screen will scroll when they scroll.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And it's fully extendable with some extra plugins to enrich the experience.&lt;br&gt;
Like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Live Share audio: talk with each other&lt;/li&gt;
&lt;li&gt;Live Share whiteboard: If you need to explain something, just draw it on the whiteboard&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why didn't anybody tell me about this? Really?! I have been using VSCode for I don't know how long. I searched for the best plugins to help me to be more productive. You know those articles telling you "The top 10 VSCode plugins you can't live without" or "20 Best VSCode Extensions To Improve Your Productivity". &lt;br&gt;
None of them EVER told me about this one.&lt;br&gt;
I'm blaming my colleagues (haha, not really or am I 🤔). This should have come up during one of our many meetups at &lt;a href="//www.frontmen.nl"&gt;the company I work for&lt;/a&gt;.&lt;br&gt;
So this little write up is for all my colleagues who didn't know about this little gem. There was at least 1, I checked.&lt;br&gt;
Or any other developer who stumbles upon this post&lt;/p&gt;

&lt;p&gt;There is so much more to VsCode Live Share, but this post is not meant as a tutorial. I just wanted sing praise and to spread the word about this amazing tool.&lt;/p&gt;

</description>
      <category>pairprogramming</category>
      <category>vscode</category>
      <category>liveshare</category>
    </item>
    <item>
      <title>Calculate HTML Element width before render</title>
      <dc:creator>Steven Straatemans</dc:creator>
      <pubDate>Fri, 19 Feb 2021 10:23:22 +0000</pubDate>
      <link>https://dev.to/sstraatemans/calculate-html-element-width-before-render-4ii7</link>
      <guid>https://dev.to/sstraatemans/calculate-html-element-width-before-render-4ii7</guid>
      <description>&lt;p&gt;I want to show you a small trick to know the size of an element, before rendering that element on the screen.&lt;br&gt;
This trick can be useful for various reasons. Developers had to use this trick a lot more in the old days, when we didn't have things like flex and CSS grid and elements couldn't size themselves the way we want them too.&lt;br&gt;
Sometimes you had to calculate the size of your element and set the width by hand.&lt;/p&gt;

&lt;p&gt;I hadn't used this trick in ages. But I ran across a small story in a recent project which made me think of this. I had tried all other CSS tricks. I asked my colleagues, but no working solution was suggested.&lt;br&gt;
And after much frustration I thought of this option.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what was the problem?
&lt;/h2&gt;

&lt;p&gt;I had to build a small component which showed 2 lines of text. So that when the user clicks the "Read more" button it will expand and show all the text.&lt;/p&gt;

&lt;p&gt;That doesn't sound complicated at all, does it? Just show X amount of words or Y amount of characters. What are you complaining about?&lt;br&gt;
Well the designer wanted to show 2 lines of text. and at the end of line 2, show the "Read more" button. Like this: &lt;br&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%2Fi%2Fstfm8ic1on9zt9zq6hom.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%2Fi%2Fstfm8ic1on9zt9zq6hom.png" alt="Output of my read-more component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We thought it was a minor task, and I think we did not even bother to poker it. Just a simple chore in a larger story.&lt;/p&gt;

&lt;p&gt;And if we would have put the button on the next line, I wouldn't be writing this article. I would just check some line-height and set the overflow of your text element to hidden and be done with it.&lt;br&gt;
But the button really, REALLY had to be at the end of that second line. Designers, right? Right?&lt;br&gt;
You can't fix that with CSS. I first thought of using float: right;&lt;br&gt;
But I would still need to know where to put the element to float. Adding it to the end of the text, would hide the button element.&lt;/p&gt;

&lt;p&gt;Somehow, we needed to find a way to know how many words we can fit on that line and also have enough space to accomodate the button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, so what is the solution?
&lt;/h2&gt;

&lt;p&gt;The easiest way to find out how many words we can fit on those two lines is to throw in one word at a time to see if it fits. And once we go over the two lines, we stop. Easy.&lt;/p&gt;

&lt;p&gt;We create a temporary element and append it to the element which will contain the actual text. Because it is a child it will &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Cascade_and_inheritance" rel="noopener noreferrer"&gt;inherit all the styles&lt;/a&gt; from our original text element, so all the text will have the correct font-size and line-height etc.&lt;br&gt;
We fill that element word by word and see if the words fit on our two lines ( + our button). And when we go over the two lines, we stop. Once we have the correct amount of text we can remove our temporary element.&lt;/p&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%2Fi%2Fir7nkrcb1pf4291huc1u.gif" 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%2Fi%2Fir7nkrcb1pf4291huc1u.gif" alt="The process of the function, finding the amount of visible text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have the correct amount of text that can fit, we copy that part of the text to the original text element that &lt;em&gt;is&lt;/em&gt; visible on your screen. And it will have your button behind it, just the way we planned it.&lt;/p&gt;

&lt;p&gt;Our function will look something like this:&lt;/p&gt;


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

&lt;p&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createMaxLines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="c1"&gt;// create the temporary Element&lt;/span&gt;&lt;br&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ruler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;ruler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;auto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;ruler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;absolute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;ruler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;whiteSpace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nowrap&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="c1"&gt;// Adding the element as a child to myElement.&lt;/span&gt;&lt;br&gt;
  &lt;span class="c1"&gt;// it will be added to the DOM&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;myElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ruler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="cm"&gt;/**&lt;br&gt;
    * Do the calculations you need to do &lt;br&gt;
    */&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="c1"&gt;// clean up after yourself&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;myElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ruler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;};&lt;/span&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;
  Will that not cause some weird flickering on your screen?&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;You would think so. We are creating an element. We are adding it to the DOM. It's why I made the temporary element invisible(with CSS) in my first version. &lt;br&gt;
But... The whole function, that checks what text should be visible on our screen, is synchronous. And there are a couple of things that are happening.&lt;/p&gt;




&lt;p&gt;But before I can explain that, we first need to look at the process of the render engine in the browser.&lt;br&gt;
There are a couple of steps that need to be taken before an element is shown on your screen.&lt;br&gt;
I will not go into complete detail here, it's to big of a subject, but if you want to learn more in depth about the rendering process, you definitely need to &lt;a href="https://www.html5rocks.com/en/tutorials/internals/howbrowserswork/" rel="noopener noreferrer"&gt;read this article from Tali Garsiel and Paul Irish&lt;/a&gt;. It's an oldy, but still awesome.&lt;/p&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%2Fi%2F0hia5j5ety4oo11xoh79.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%2Fi%2F0hia5j5ety4oo11xoh79.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So first the DOM tree is created, containing a tree with all our HTML tags. Also the CSS is parsed in such a tree.&lt;br&gt;
These two are combined in the render tree, where styles and elements are combined.&lt;br&gt;
The next step is the layout or reflow, where all elements will receive their position. &lt;br&gt;
And finally the paint stage, where the elements will appear on the screen.&lt;br&gt;
Now every time an element is added to the DOM, like in our function, the position of all elements needs to be recalculated in the layout/reflow stage. When that stage is done the screen will be repainted.&lt;br&gt;
Like I said read the article mentioned above for details, what I described here was a gross over-simplification.&lt;/p&gt;




&lt;p&gt;As soon as our temporary element is added to the DOM it will trigger a reflow of the render engine. &lt;br&gt;
Now, every time a word is added to the element another reflow is triggered. BUT...not a repaint. The repaint will occur at the end of our function when every calculation is done. And this is the important part, because it is the repaint that will make everything appear on you screen. But at the end of our function, we will remove the temporary element from our DOM, again causing a reflow. Only after that reflow, will the paint part of the render engine run. And because our temporary element is not in the DOM anymore, it will not appear on our screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  How about performance?
&lt;/h2&gt;

&lt;p&gt;You shouldn't try it with the whole content of "War and Peace", but this option is usually done with just a couple of lines of text and that should be fine.&lt;br&gt;
You can probably improve the performance somewhat, by using a better algorithm to determine how many words will fit.&lt;/p&gt;

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

&lt;p&gt;This is a nice little trick if you need to calculate the size of your element before it will show on your screen. &lt;br&gt;
You will not need it much, because most scenarios nowadays, you can solve with CSS. But for those rare occasions CSS can't help you out, this might do the trick.&lt;br&gt;
Would love to hear from you when you used it in one of your own projects. &lt;/p&gt;

&lt;p&gt;I created a small react-component for it, so if you're curious you can find the &lt;a href="https://github.com/sstraatemans/react-read-more" rel="noopener noreferrer"&gt;code here&lt;/a&gt; and &lt;a href="https://react-read-more.sstraatemans.vercel.app/?path=/story/readmore--max-lines" rel="noopener noreferrer"&gt;example here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>width</category>
      <category>javascript</category>
      <category>html</category>
      <category>dom</category>
    </item>
  </channel>
</rss>
