Building Reusable Components That Are Actually Resuable
Building Reusable Components That Are Actually Resuable
Twitter: @housecor
Consulting: reactjsconsulting.com
Courses: pluralsight.com/author/cory-house
As society advances, we
take more things for
granted.
Reusable Component Decisions
Core Decisions Design Testing
Core technology Wrap HTML primitives? Framework
Audience & support team model Folder structure Assertions
Declare propTypes? Helpers
Rigid or flexible
Apply custom props? Unit
Link, wrap, or fork Defaults? Interaction
Accessibility Structural
Dev Environment Server rendering Style
50 Decisions
Boilerplate, doc lib, or custom Config object vs. primitives When to run
Library or standalone Atoms Where to place tests
Monorepo or separate Molecules Continuous Integration
Organisms
Documentation Deployment
Styling Dependency declarations
Off-the-shelf or custom
CSS Technology Hosting
Generating documentation Inline vs external Versioning
Search Namespace Import approach
Syntax highlighting Naming scheme Declaring package files
Interactive playground Offer unstyled components? Output formats
Hosting Theming Pre-publish testing
Automated deployment Build process
This is about to get So, I’m a big fan of TypeScript…
interesting…
Project
Team
Business
Public
Audience
Public
Business
Team
Start here
Project
Difficulty
Start small.
Start specific.
Decision:
Rigid or flexible?
Style
- Colors
- Padding
- Size
- Font
Layout
- Horizontal vs vertical
- Media query cutoff points
Data display
Logic
Rigid Flexible
Easier to understand
Easier to test
Product Page
NavLink AuthorCourses
CourseSummary
StarRating
SidebarNav
AuthorSummary
React: Nested Components
atomicdesign.bradfrost.com
Difficulty
Reuse
Atom
Molecule
Atom
1. Atoms
2. Molecules
3. Organisms
4. Templates
5. Pages
Atomic design = hierarchy.
Decision:
Link, wrap or fork third parties?
Link Wrap Fork
Here’s a suggestion Pretend we built it Own it
Link
// DatePicker.js
import SuperDate from ‘super-date’;
Documentation Integrated
Changes Choose when to upgrade
New features Choose when to upgrade
Customization Some. Enhance / abstract
Styling Varies
Cohesion Varies
Commitment Low
Ease Medium
Fork
Documentation Integrated
Changes Full control
New Features Your responsibility
Customization Full control
Styling Built-in styles
Cohesion High
Commitment High
Ease Hard
Fork
Wrap
Start here.
Link
Horizontal or vertical?
Errors at top, by field, or both?
Show errors on blur or submit?
Mark required fields? If so, how?
Tie label to input via htmlFor?
Padding below each input?
TextInput Opinions
Designed for private use
Vertical layout
Padding below
Required htmlFor for accessibility
Red asterisk if required
Errors in red below the input
Could add…
Textarea support
Automatic placeholder
Validation onBlur
Demo
Decision
Fatigue
What you make unconfigurable is as
important was what you make
configurable.
Decision:
When should I add a component to
the library?
“A reusable component should be tried out
in three different applications before it will
be sufficiently general to accept into a
reuse library.”
Jeff Atwood
“Once we proved that the Stack Overflow engine
could scale to these three distinct communities, I
was comfortable pursuing Stack Exchange, which
is now a network of
over 100 community-driven Q&A sites.”
Jeff Atwood
Rule of Three:
Try a component in three different spots
before placing it in a reuse library.
Create a “reusable” folder
Central place to store app-specific reusable components
Decision:
Styling
React Styling Approaches
Themeable Yes Yes, change variables Yes, but no standard Not easily Varies
Navigation
Example
CodeExample
Props
Dev Environment Options
React Storybook
React Styleguide
Advantages Disadvantages
Complete control Undiscoverable
Centralized asset hosting Must grant access
Avoid separate fees for each type Must configure registry setting
Easier on-boarding / management
Let’s wrap up.
Reusable Components - Recap
Audience Start small
Rigid vs Flexible Start rigid
Atomic Design Start with atoms. Then Molecule, Organism
Link, wrap, or fork? Link if you can. Fork if you must.
Wrap HTML primitives Programmatically enforce standards
When to add Rule of three
Styling approach See “Creating Reusable Components”
Documentation Generate it
Open/inner source Consider inner sourcing
Package hosting Consider asset repos
PLURALSIGHT.COM
reactjsconsulting.com