<?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 Jonas Wolfram on Medium]]></title>
        <description><![CDATA[Stories by Jonas Wolfram on Medium]]></description>
        <link>https://medium.com/@jonas_wolfram?source=rss-13fa8eeea59d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*z3K12FKrczbfWxDYBFhAFA@2x.jpeg</url>
            <title>Stories by Jonas Wolfram on Medium</title>
            <link>https://medium.com/@jonas_wolfram?source=rss-13fa8eeea59d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 08:17:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jonas_wolfram/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[How to Use Uniswap to Swap Tokens From a Custom Frontend]]></title>
            <link>https://medium.com/better-programming/how-to-use-uniswap-to-swap-tokens-from-a-custom-frontend-6e608dbaad22?source=rss-13fa8eeea59d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e608dbaad22</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[uniswap]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[web3]]></category>
            <dc:creator><![CDATA[Jonas Wolfram]]></dc:creator>
            <pubDate>Thu, 10 Nov 2022 09:11:10 GMT</pubDate>
            <atom:updated>2022-12-13T06:48:12.148Z</atom:updated>
            <content:encoded><![CDATA[<h4>Let’s build our own dApp</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eeJUTEz1MmcrykE5" /><figcaption>Photo by <a href="https://unsplash.com/@dengxiangs?utm_source=medium&amp;utm_medium=referral">Choong Deng Xiang</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>How cool would it be to build your own dApp which lets you swap all kinds of tokens without actually having to write a single line of Solidity code? Well, with the Uniswap SDK you can use Uniswap’s smart contracts to swap a whole bunch of ERC20 tokens with your own custom frontend.</p><p>In this tutorial, we are going to build a React dApp which lets you swap ETH for UNI.</p><p>The tutorial focuses on the actual logic and how to connect to it from the user interface with the default user agent CSS. It does not contain any custom styles. However, I also created a styled version of the dApp.</p><p>If you are interested in how that has been done head over to the bottom of the page where I linked the repository and the actual website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*3mJs7CJ9qT9ki6G6TYUxMA.png" /><figcaption>Swap for ETH for UNI using Uniswap in the background</figcaption></figure><p>In order to follow the tutorial basic knowledge of npm, React, ethers.js and wagmi.sh is needed.</p><p>First, we need to create our React dApp. We will use TypeScript for this project. So open up your terminal and write:</p><pre>npx create-react-app uniswap-dapp --template typescript</pre><p>Next, change the directory to the newly created one and install the dependencies:</p><pre>cd uniswap-dapp<br>npm install ethers wagmi @uniswap/v3-sdk @uniswap/sdk-core</pre><p>Now start the development server.</p><pre>npm start</pre><p>Your default web browser should open up and display the infamous React screen.</p><p>Next, open the newly created project in your favourite code editor. To start with a clean setup delete all boilerplate code. Delete everything from the files index.css and App.css. Delete the logo.svg file and remove most things inside the App.tsx file so it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47d4a7b4626cb0ec9da4f9db61bc5567/href">https://medium.com/media/47d4a7b4626cb0ec9da4f9db61bc5567/href</a></iframe><p>Now we can get started!</p><h3>Wallet connection</h3><p>We are going to use the library wagmi.sh for an easy wallet connection to MetaMask. If you already know how to set up a wallet connection with MetaMask and wagmi.sh feel free to skip this section.</p><p>In order to connect our app with an Ethereum node and be able to access blockchain-related information from anywhere within our application we have to wrap our entire application with the WagmiConfig provider and pass its configuration options. Head over to App.tsx and change the file like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78b734391c5344444a7c39449edf906a/href">https://medium.com/media/78b734391c5344444a7c39449edf906a/href</a></iframe><p>For this demonstration, we will use the Goerli testnet. We also use a public provider since we don’t have to send a lot of transactions. If your dApp exceeds the rate limits of the public provider you need to use services like Alchemy or Infura instead.</p><p>With that, we are successfully connected to an Ethereum node and could already read data from the blockchain. However, in order to make transactions and change the blockchain state we need to connect our dApp to a wallet.</p><p>We will implement our wallet connection button and logic within the header of the dApp. Create a new Header component inside a new directory called components with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8257e220f301d285e51912ebd7738a75/href">https://medium.com/media/8257e220f301d285e51912ebd7738a75/href</a></iframe><p>The InjectedConnector refers to the MetaMask extension and we make sure that MetaMask uses Goerli as its chain by passing the chainId property with its value set to Goerli’s id. Now we should be able to connect our MetaMask wallet to the dApp with this beautiful button that appeared on the page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OIzqs4PiNQNCAUKEzQnioA.png" /></figure><p>After a successful connection, the button turns into the currently connected wallet’s address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Z5JBX-WvVu2epIiLZZhZQ.png" /></figure><h3>Fetching spot prices</h3><p>Now that the wallet connection has been taken care of we can focus on the actual topic of this article: How to use Uniswap to swap tokens from your own custom frontend. We will start by creating a new custom hook called useSwap in a new directory called hooks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/981af42a71613d80449a6fbc9f87d852/href">https://medium.com/media/981af42a71613d80449a6fbc9f87d852/href</a></iframe><p>In order to fetch the token prices we need to interact with the contract of a pool that includes this token pair. As said in the beginning we are going to interact with the ETH-UNI pool. Internally ETH is handled as wrapped Ether or WETH in short because Uniswap can only handle ERC20 tokens and ETH itself is no token but a currency. So the pool that we are looking for is actually a WETH-UNI pool. I will explain this in more detail later on. For now we will get a reference to the pool contract like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a9d5cf36d7e9d60f1a2937d4ec51503/href">https://medium.com/media/7a9d5cf36d7e9d60f1a2937d4ec51503/href</a></iframe><p>Since we will only read from that contract and make no transactions it’s enough to use a provider rather than a signer (a provider with a wallet connected to it).</p><p>Next up we create two interfaces Immutables and State modelling the on-chain pool data that we need.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c767a1f9dd70722031c6b1dd25ac175/href">https://medium.com/media/6c767a1f9dd70722031c6b1dd25ac175/href</a></iframe><p>We create two functions getPoolImmutables and getPoolState which do the actual data fetching and return the data corresponding to their respective interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7fe91bceb238e2cb8a7860965e82db8/href">https://medium.com/media/b7fe91bceb238e2cb8a7860965e82db8/href</a></iframe><p>With that, we already have all the blockchain data that we need in order to make a quote.</p><p>We add two more constants to the top of the file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef5e97d561476b09bcae7677980872b2/href">https://medium.com/media/ef5e97d561476b09bcae7677980872b2/href</a></iframe><p>And now we can implement the getQuote function which returns the amount of UNI we get for a specific amount of ETH.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10968815fc1cf4cb65237cce379a6c99/href">https://medium.com/media/10968815fc1cf4cb65237cce379a6c99/href</a></iframe><p>The function calls the two other functions that we defined earlier in order to get the current blockchain data. What is interesting to note here is that the token0 of the pool is UNI and the token1 of the pool is WETH. After that Uniswap&#39;s Token and Pool classes are instantiated with the returned data. Finally, we get the quote by multiplying the amount of ETH with the price of UNI with respect to ETH.</p><p>Putting all the pieces together and returning the getQuote function from the component your file should now look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fb9a04ed553b548ab3c111a0cc264b4/href">https://medium.com/media/7fb9a04ed553b548ab3c111a0cc264b4/href</a></iframe><p>We can now start to build the UI which calls the function and updates according to the returned quote. Create a new component named SwapCard with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f53d3d3f2ac1c99b91a52ad1f938152a/href">https://medium.com/media/f53d3d3f2ac1c99b91a52ad1f938152a/href</a></iframe><p>Now if you type any number in the input field for ETH the expected amount of UNI you would get if you were to execute the swap gets displayed in the second input field. Your website should now look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lN21c8p3ACCMMbCrqloxkw.png" /></figure><h3>Turn ETH into WETH and approve for spending</h3><p>Now before we can actually ask the Uniswap v3 router to swap our ETH to UNI we have to convert our ETH into WETH and allow the router contract to use WETH on our behalf. As I mentioned earlier there is actually no pool which includes ETH directly. Uniswap just works with ERC20 tokens which is why we need to convert our ETH to the ERC20 token which represents ETH that is WETH.</p><p>So we start by creating a new file called useWETH.tsx which is a custom React hook just like useSwap.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/105627177cef558b9e5255278a406bf7/href">https://medium.com/media/105627177cef558b9e5255278a406bf7/href</a></iframe><p>Next, we want to create the contract instance of the WETH token. For that we need the token ABI. Create a new directory called abis and a new file called WETH.json in the directory with this content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f747a159519c31a28fc64fa720d7406/href">https://medium.com/media/7f747a159519c31a28fc64fa720d7406/href</a></iframe><p>You can also copy the ABI directly from <a href="https://goerli.etherscan.io/address/0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6#code">Etherscan</a>.</p><p>Now we’re ready to instantiate the contract like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9ea61de2878216a61c25639e026f3b8/href">https://medium.com/media/c9ea61de2878216a61c25639e026f3b8/href</a></iframe><p>We add an additional constant at the top which represents the number of decimals of the WETH token.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a41a86f1993cd99daacb578a13734898/href">https://medium.com/media/a41a86f1993cd99daacb578a13734898/href</a></iframe><p>Next up we create a function called deposit which parses the input amount and calls the deposit function on the WETH contract. With the help of this function, we convert our native ETH into WETH tokens that we can then use for token swapping.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13ab2f8db01222e34282af0a92f481ae/href">https://medium.com/media/13ab2f8db01222e34282af0a92f481ae/href</a></iframe><p>Now we just need to create one more function and we have all prerequisites to be able to execute a swap. Create the approve function. In order for the Uniswap router contract to access our WETH it needs to have permission to do so.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a83817051f573b65456740a6d1ef5ed/href">https://medium.com/media/9a83817051f573b65456740a6d1ef5ed/href</a></iframe><p>Within our approve function we call the WETH contract’s approve function and define how many tokens the router can access.</p><p>Now we’re all set to write the logic for the swap.</p><h3>Execute the swap</h3><p>Head over to the useSwap.tsx file and add this import:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/188eb13bf3260c261464cbc216527e59/href">https://medium.com/media/188eb13bf3260c261464cbc216527e59/href</a></iframe><p>Now, add this constant to the top of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7c97cea79d71000ff134f75c12e3613/href">https://medium.com/media/a7c97cea79d71000ff134f75c12e3613/href</a></iframe><p>And the following under the already existent useProvider hook:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb7653fed53855cc3ee705df595bb66a/href">https://medium.com/media/cb7653fed53855cc3ee705df595bb66a/href</a></iframe><p>Because we will change the state of the blockchain we need a signer rather than a provider for the router contract. Additionally, we import the functions we wrote in the useWETH hook.</p><p>Now lastly create the swap function which executes the swap.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5eaa4a467a45ab468ada976790cb7b5f/href">https://medium.com/media/5eaa4a467a45ab468ada976790cb7b5f/href</a></iframe><p>This function accepts the amount in ETH that needs to be swapped as an argument. It then converts ETH to WETH with the deposit function we defined earlier and approves the Uniswap v3 router to access exactly that amount in WETH with the help of the approve function.</p><p>It then gets the necessary pool data (token addresses, fee) with the getPoolImmutables function and constructs a params object which will be passed as an argument to the transaction.</p><p>Finally, we call the exactInputSingle function on the Uniswap router contract which executes the swap. A custom gasLimit has to be set because MetaMask can’t estimate the gas fee on this transaction.</p><p>You can now add the swap function to the return statement and your file should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62cd6bf382e480b4d5351c72ea21573c/href">https://medium.com/media/62cd6bf382e480b4d5351c72ea21573c/href</a></iframe><p>Now head over to the SwapCard.tsx component, add the logic to save the value of the input in the state and add a button which initiates the swap with said amount. Your file should now look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57559836c8a02914b7315024444f72f2/href">https://medium.com/media/57559836c8a02914b7315024444f72f2/href</a></iframe><p>Additionally, I added the behaviour that the swap button is only enabled if a wallet is connected.</p><p>Now head over to localhost:3000 to test it because everything should be working by now. Just make sure that you actually have enough ETH on the Goerli network.</p><p>If you need more test ETH head over to a Goerli <a href="https://goerlifaucet.com/">faucet</a> like the one from Alchemy. Also, keep in mind that it might take a while for the 3 transactions to be mined and that we do not have any user feedback about the current transaction status as of right now.</p><h3>Display the current ETH and UNI balance</h3><p>To directly see how much ETH you are able to swap and to get some sort of feedback on the result of the swap it would be nice to display the current balance. Luckily with wagmi.sh it is incredibly easy to do so.</p><p>Add this constant to the top of the SwapCard.tsx file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cc647db50b3b1b9e4cb6421a1466588/href">https://medium.com/media/6cc647db50b3b1b9e4cb6421a1466588/href</a></iframe><p>Then, add this under the useAccount hook:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72d76768f926972d36ab5087f06ab1ce/href">https://medium.com/media/72d76768f926972d36ab5087f06ab1ce/href</a></iframe><p>The watch property makes sure that our balance is always up-to-date by checking if the balance changed after each new block. Now last but not least we add a little note in the template for both tokens like this one for ETH:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/accc274029f62fa7eb09841abc6a47bf/href">https://medium.com/media/accc274029f62fa7eb09841abc6a47bf/href</a></iframe><p>Now putting all things together your file should look somewhat similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b0f9985ef8a756a399f4c76e6edc6af/href">https://medium.com/media/4b0f9985ef8a756a399f4c76e6edc6af/href</a></iframe><p>Our unstyled dApp should now look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TlIKVoG2ozHujvntLe3w1Q.png" /></figure><p>If you try to swap tokens now you will see that after a successful swap the balance of both tokens changes accordingly.</p><p><strong>Congratulations!!!</strong></p><p>You successfully built a dApp which lets you swap your ETH for UNI. Building upon this you could make it more pretty, add validation, user feedback, more tokens etc. The possibilities are endless.</p><p>This tutorial only focussed on the logic behind swapping tokens. Without CSS it looks bland which is why I added styles for the dApp we built in this tutorial.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ErXCGRHd0mV6Xe90qxkb0A.png" /><figcaption>Tutorial logic with additional styles (check links below)</figcaption></figure><h3>Source Code and Demo</h3><ul><li>If you want to check out the whole code head over <a href="https://github.com/JonWofr/uniswap-dapp-unstyled">here</a>.</li><li>If you want to check out the whole code including styles and some more additions and adjustments head over <a href="https://github.com/JonWofr/uniswap-dapp-styled">here</a>.</li><li>If you want to get a demo of the dApp including styles and some more additions and adjustments: I deployed it on Vercel <a href="https://uniswap-dapp-styled.vercel.app/">here</a>.</li></ul><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e608dbaad22" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/how-to-use-uniswap-to-swap-tokens-from-a-custom-frontend-6e608dbaad22">How to Use Uniswap to Swap Tokens From a Custom Frontend</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rendering line breaks from JavaScript strings]]></title>
            <link>https://medium.com/@jonas_wolfram/rendering-line-breaks-in-dynamic-content-in-react-27cbbce1d9d?source=rss-13fa8eeea59d------2</link>
            <guid isPermaLink="false">https://medium.com/p/27cbbce1d9d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Jonas Wolfram]]></dc:creator>
            <pubDate>Thu, 30 Jun 2022 03:46:40 GMT</pubDate>
            <atom:updated>2022-07-01T01:01:10.509Z</atom:updated>
            <content:encoded><![CDATA[<p>In static HTML it’s possible to just use the &lt;br /&gt; tag to force a line break. If that content however is dynamically injected via JavaScript it’s not as trivial.</p><p>Consider following Pen created with React:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FzYWxryx%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DzYWxryx&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FzYWxryx&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FzYWxryx-512.jpg%3Fversion%3D1656553064&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/aaa27c0af4d397fa8775aa1a66f92084/href">https://medium.com/media/aaa27c0af4d397fa8775aa1a66f92084/href</a></iframe><p>In the pen you can see that the card component gets dynamically rendered depending on the items in the cards array. We decided that the description of the first card should contain a line break between the first and second sentence. That’s why we included the &lt;br /&gt; tag. But as you can see the HTML tag just gets rendered in plain text. One might then try to use the new line character \n to get the job done.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FrNdaeWe%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DrNdaeWe&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FrNdaeWe&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FrNdaeWe-512.jpg%3Fversion%3D1656554880&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8a52e7bed301a65871a1e2c72d3a97c/href">https://medium.com/media/b8a52e7bed301a65871a1e2c72d3a97c/href</a></iframe><p>But as you can see that just gets ignored. We have to change our underlying code a bit in order to support means to indicate a line break.</p><h3>innerHTML</h3><p>With the help of the innerHTML property of a DOM element a string of HTML can be dynamically injected into an element the same way as if it the content would be plain HTML. In React you can use thedangerouslySetInnerHTML property on elements which sets the element’s innerHTML DOM property under the hood. To achieve what we want we could use:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FjOzEqwN%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DjOzEqwN&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FjOzEqwN&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FjOzEqwN-512.jpg%3Fversion%3D1656557254&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/00f929cb244f9d289b1a086f6db765f2/href">https://medium.com/media/00f929cb244f9d289b1a086f6db765f2/href</a></iframe><p>However usage of the property is discouraged because it can lead to XSS attacks if the dynamic content is not properly sanitized. That’s why for something as simple as a line-break I think it’s not a good option.</p><h3>pre-line</h3><p>The CSS property white-space set to the value pre-line makes it possible to use the new line character \n to trigger a line break.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FMWVYyLj%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DMWVYyLj&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FMWVYyLj&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FMWVYyLj-512.jpg%3Fversion%3D1656558063&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/58656c839eeb387942405949657caf80/href">https://medium.com/media/58656c839eeb387942405949657caf80/href</a></iframe><p>This approach requires minimal changes to the existing code and gets the job done pretty well. However if we just inspect the HTML code in the inspector we can‘t see anything that indicates a line break.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0IOGzhuJQ_vRG0bHf2Z-cA.png" /></figure><p>This might be confusing because we get a different HTML markup for dynamic content with line breaks than static content with line breaks. Also I think a line break shouldn’t be tied to CSS. It should be something that is solely defined in the content rather than the styling. That’s why I believe the third option might be even better.</p><h3>innerText</h3><p>The DOM property innerText turns every new line character into a &lt;br /&gt; element. This means that we do not have to change any CSS and we do not have to use any HTML markup in our string in order to trigger a line break. Sadly React doesn’t offer a convenience attribute like dangerouslySetInnerHTML for this DOM property. That’s why we have to write a few more lines of code:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FXWEJKmz%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DXWEJKmz&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FXWEJKmz&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FXWEJKmz-512.jpg%3Fversion%3D1656559503&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6c1b44086a2a689741ee0ee7e02af24/href">https://medium.com/media/e6c1b44086a2a689741ee0ee7e02af24/href</a></iframe><p>We can get a reference to the DOM element with the help of the ref attribute. By providing it a callback it gets triggered every render. We can then set the innerHTML property on that element. If we have a look at the code in the inspector we will see that it is exactly the same as it would be if it would not have been dynamically rendered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*txggNNxE-Q7JrYKGV4W4mw.png" /></figure><p>That’s three different approaches trying to solve one supposedly simple problem. Which one do you use to solve the issue and did I forgot one? Let me know in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27cbbce1d9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rounded gradient borders in CSS]]></title>
            <link>https://medium.com/@jonas_wolfram/rounded-gradient-borders-in-css-6cfefd754281?source=rss-13fa8eeea59d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cfefd754281</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Jonas Wolfram]]></dc:creator>
            <pubDate>Fri, 10 Jun 2022 09:55:26 GMT</pubDate>
            <atom:updated>2022-06-10T10:48:22.212Z</atom:updated>
            <content:encoded><![CDATA[<p>Did you ever run into issues setting a gradient as the border color of a rounded button?</p><p>Well what’s easy to accomplish in Design tools like Figma is actually pretty hard to do in CSS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/125/1*1xPm8Nb2gq5OFshVlX45Gg.png" /><figcaption>This is what the button should look like</figcaption></figure><p>The reason why it’s cumbersome in CSS is because the set of border related properties is not suitable for the combination of a gradient border and rounded corners. The following pen demonstrates the issue.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FmdXzpYj%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DmdXzpYj&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FmdXzpYj&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FmdXzpYj-512.jpg%3Fversion%3D1654759506&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d5fb22bc3517c05b64d9f27fa4e6823/href">https://medium.com/media/1d5fb22bc3517c05b64d9f27fa4e6823/href</a></iframe><p>You can see that the border-radius property gets ignored at least visually. It seems as if the border radius does simply not get taken into account. A quick Google search revealed that the two properties namely border-image and border-radius are not compatible with each other. So we have to find a workaround.</p><p>The are multiple ways to approach this all with their own pros and cons. In the following I will elaborate the different approaches and in what situation each approach might be suitable.</p><h3>Multiple clipped backgrounds</h3><p>One approach is to stack two backgrounds on top of each other. The bottom one is the gradient background which gets clipped to the border box of an element. The top one is a background colored in the same color as the document background which gets clipped to the padding box of the element.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FExQOjvv%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DExQOjvv&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FExQOjvv&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FExQOjvv-512.jpg%3Fversion%3D1654839259&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/235ec8926650dcca9f7554f5c994bc1f/href">https://medium.com/media/235ec8926650dcca9f7554f5c994bc1f/href</a></iframe><p>The result is a button which looks exactly like the design mockup. However there is one drawback. The issue is that we simply create the illusion of the button being hollow by using the same color as the document background for the top background image. Now if the document background would change we might have a problem with the background of the button not changing accordingly which would show that the button actually isn’t hollow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*Rof6MoJXFT4hbyk1uLwu5Q.gif" /><figcaption>A change in the document background color shows that the button actually isn’t hollow</figcaption></figure><p>A way to solve this might be CSS variables. However it will always remain a “hacky” solution.</p><h3>Pseudo element with background</h3><p>Another approach uses a pseudo element behind the actual button with a gradient background. The button itself should have a background color similar to the document color. Once again with that approach the illusion of a hollow button gets created.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FRwQqPYd%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DRwQqPYd&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FRwQqPYd&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FRwQqPYd-512.jpg%3Fversion%3D1654840788&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/78ee4fe79cb768f61afc7e2d25f0ebe3/href">https://medium.com/media/78ee4fe79cb768f61afc7e2d25f0ebe3/href</a></iframe><p>The advantage of this approach over the previous one comes down to state changes. If for example a hover state should make the border darker it is a lot easier to simply apply the effect property on the pseudo element rather than messing with background related properties on the button element itself.</p><h3>CSS masking</h3><p>The third solution involves the relatively new CSS technique which is masking. Masking however is still not a CSS standard and vendor prefixes must be used to ensure compatibility. With masking we actually create a hollow button that does not just seem hollow. Applying a mask on an element means that only areas of the element which are defined in the mask will be visible afterwards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*RZpEWnxW5RoS95tbzzJRcA.png" /><figcaption>The button without masking the background of the pseudo element</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*qJGHYWyeSjqfU05Elp8NZw.png" /><figcaption>The mask</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*APzUkmQrKORps7AO8RJa6g.png" /><figcaption>The button with the background of the pseudo element being masked</figcaption></figure><p>In this particular example we can simply use a pseudo element with the linear gradient as a background (same as in the previous approach) and then cut out everything that is not wanted with the mask shown above.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fembed%2Fpreview%2FZErmWoe%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DZErmWoe&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2FJonWolf%2Fpen%2FZErmWoe&amp;image=https%3A%2F%2Fshots.codepen.io%2FJonWolf%2Fpen%2FZErmWoe-512.jpg%3Fversion%3D1654852278&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a583687646a6a9e4059106a9f97a2f21/href">https://medium.com/media/a583687646a6a9e4059106a9f97a2f21/href</a></iframe><p>In the code you can see the mask-image property which is set to the data URL of the SVG shown in the image above. A URL to an external SVG can also be provided however inlining the SVG by transforming it to a data URL means that no additional roundtrip to the server is necessary to use the SVG. Browser support is already pretty huge with around 95% coverage if additionally to the mask-image property the -webkit-mask-image property is provided.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*UYY-w6q3z6-9097p4N9DFA.gif" /><figcaption>This button has the desirable appearance and is actually hollow</figcaption></figure><p>This approach might be the most difficult one while also being the cleanest. It matches the appearance and behaviour that we see in design tools like Figma. Also since the background is set on the pseudo element we can easily add state transitions. The only downside might be that in order to get it right the mask has to be tweaked and some SVG knowledge might be necessary.</p><p>What do you think? Is there an approach I haven’t thought of yet or is there a more elegant way of achieving it? Let me know in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cfefd754281" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>