One of the responses I got to yesterday's article about how I built my own code sandbox Web Component was a bit of awe at the simple truck I used to handle live syntax highlighting. A lot of developers struggle with building simple solutions. But my code rarely starts off simple. It usually starts off as a hot mess of "wow it's finally working!", and takes a few rounds of revisions to get something simple, clean, and maintainable. Today, I wanted to talk about that journey! Let's dig in… A recapIf you didn't watch the whole thing (it's long, I don't blame you!), the rendered output is a Here's a simplified version…
The The only difference between the two is that the
Whenever the user types in the That highlighted text "shows through" the transparent Because everything is the same size and font, all of the characters line up, they can copy/paste intuitively, and so on. It works really well, but it was the last of many attempts I made at this! How other tools handle thisI started by researching how other code sandbox tools handle this. SandpackJosh Comeau's courses use Sandpack, a React-based library for embedding CodeSandbox editors. Sandpack uses a I tried this approach initially, but found it janky as hell and prone to lots of weird edge case bugs. Obviously, Sandpack has all of those ironed out. But addressing them meant throwing a lot more JavaScript at the problem than I wanted to. It feel overengineered and antithetical to what I teach. CodePenLike my approach, CodePen uses a This works wonderfully… but again adds a ton of complexity and extra JavaScript. The blinking cursor? It's a Because the It's brilliant engineering! But there's just so much than can go wrong! How I landed on my approachIn a phrase: trial-and-error! I started building out by using I found CodePen's approach solved a lot of those problems, but introduced some new ones. The whole thing that got me to "just overlap them" started because I didn't want to have to code my own blinking cursor when the browser just gives you that for free with a I don't think I came up with the idea myself. I'm pretty sure I stumbled upon a throwaway StackOverflow comment that suggested it as an idea in response to someone else's question. But I'll be damned if I could find the source again! I initially didn't even both with positioning or hiding things. I started off with…
If you find yourself struggling with complexity, it's good to remind yourself that you don't need to just magically have the answers. I watch a lot of engineering and science YouTube (not software engineering, real engineering). One thing they all have in common is they try a lot of shit before they land on something that works. Idea => Experiment => Fail => Learn => Repeat. Every time you fail, you learn something new. You can use what you learn to try a different approach, and learn some more. Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] The code complexity journey"