Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Render 8 min read How-to mediumAdept

Design Patterns for Building Reusable Svelte Components

Reusable Svelte components benefit from four patterns, illustrated with the open-source svelte-bar-chart-race library: component composition via slots, two-way binding, slot overrides, and communicating through TypeScript types. Splitting the library into a parent BarChartRace that shares state through the Context API and stores, plus composable Chart and Slider children, gives consumers control over layout instead of a single component with many object props. Slot props via let: expose state and setters, default slots act as override escape hatches, and typed stores, typed context and SvelteComponentTyped definitions (auto-generated by svelte-kit package via svelte2tsx, or hand-written) improve correctness and IDE support.

Assumes: Svelte basicscomponent props and slotsTypeScript basics

Related posts

  • This Week at Zed Industries: #16

    Zed mediumAdept

    Zed has started using its new channels feature internally, where members can message, talk, share screens and co-edit projects, and plans to use it to stream development and host office hours once the editor is open-sourced. Alongside this, GPUI layout and styling are being reworked with the taffy layout engine and Tailwind-like style methods. Antonio proposes `auto` and `prettier` options for the formatter setting. Mikayla walks through a new composable Component primitive for GPUI in Rust: a builder-style trait consumed into an Element, a StylableComponent trait with associated Style/Output types, and the Typestate pattern using `()` as an unbound style, so wrappers like Hover and Toggle compose type-safely and rendering an unstyled component fails at compile time.

  • Building Custom Animations in the Workflow Builder

    Slack Engineering mediumAdept

    Slack's Workflow Builder added drag-and-drop from a new steps sidebar into workflows by moving react-beautiful-dnd's DragDropContext to wrap the whole builder page, with two draggable/droppable lists. A custom context provider holds drag state fed by the library's responders. On top of that the team built several workarounds: custom transforms to re-center differently sized steps, DOM-measured dynamic placeholders the library doesn't support, and a velocity-based tilt effect driven by requestAnimationFrame. A key lesson was hiding hint boxes in onBeforeCapture rather than onBeforeDragStart, because only the former runs before the library measures the DOM.

  • Hydrogen & Tailwind: The Perfect Match for Building Beautiful Storefronts

    Shopify Engineering mediumAdept

    Tailwind's utility classes ship as the default styling option in Shopify's React-based Hydrogen storefront framework, and the case for them rests on componentization: styles live in reusable React components rather than semantically named classes like BEM-style product-card__title, so layouts can be copied or extracted (e.g. ProductCard to BlogCard) without renaming or duplicating CSS. Further arguments include enforced spacing and color constraints for visual consistency, copy-paste composability from libraries like Tailwind UI, easier team collaboration, and not having to name things. The tradeoff is a real learning curve that still requires CSS fundamentals, eased by good docs and the VSCode extension.

  • How we use Web Components at GitHub

    GitHub Engineering mediumAdept

    GitHub moved from jQuery to native Web Components starting in 2014 and now ships dozens of custom elements such as <include-fragment>, <details-dialog> and <relative-time>. Engineers author components in the Rails monolith with Catalyst, a TypeScript decorator library, paired one-to-one with ViewComponent on the server, and enforce practices with ESLint plugins and ratchet tests that block deprecated patterns like facebox. Proven components are generalized, stripped of Catalyst and published as dependency-free, style-free open-source elements with TypeScript definitions. One example is <typing-effect>, built in under 40 lines to avoid a fivefold bundle-size increase from typed.js.

  • Fluid layout customization with Splits

    Replit mediumAdept

    Replit's Workspace now supports Splits, a tiling layout where tabs, panes and files from the filetree can be dragged onto five hit areas of any pane (header to merge, or four conical-section quadrants to split), plus maximize and float modes. The design favours predictability, learnability and fluidity: explicit state visualization, a visible 50px detach radius, continuous resizing during drag, and few special cases, such as always placing auto-created webviews in the top-right node. To get fluid drag behaviour in React, the team used jotai to skip rerenders during drag state updates and wrote custom low-level mouse handlers instead of using a tiling library. Layout is stored as a multi-node tree of Splits and PaneGroups, which drives resize behaviour and simple placement heuristics.

The quest for software engineering knowledge.

Look:
Layout: