Slide 1

Slide 1 text

The dream that turned into nightmare !
 ..and how we wake up Radoslav Stankov

Slide 2

Slide 2 text

!

Slide 3

Slide 3 text

Radoslav Stankov @rstankov rstankov.com

Slide 4

Slide 4 text

https://tips.rstankov.com

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

... in a start up

Slide 8

Slide 8 text

... far far way

Slide 9

Slide 9 text

... in Silicon Valley

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

"Dream" is short for "the dream to not write custom CSS". Let's hope v2 is not named "Nightmare" ! Goals - Have consistent UI on the website - Make it easier to build new pages - Implement Product Hunt v6 design - Fully type safe What - Foundational utilities - Core UI components

Slide 14

Slide 14 text

"Dream" is short for "the dream to not write custom CSS". Let's hope v2 is not named "Nightmare" ! Goals - Have consistent UI on the website - Make it easier to build new pages - Implement Product Hunt v6 design - Fully type safe What - Foundational utilities - Core UI components !

Slide 15

Slide 15 text

Core Design System - 4px grid - font size - font weights - color tokens - layouts

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

The core components part

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

import { Flex } from 'ph/components/dream'

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

import { Layout } from 'ph/components/dream'

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

import { Button } from 'ph/components/dream'

Slide 30

Slide 30 text

import { Button } from 'ph/components/dream'

Slide 31

Slide 31 text

const onClick = useOnClick(props);

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

import { Form } from 'ph/components/dream'

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

import { Text } from '~/components/dream' Title Subtile Bold

Slide 39

Slide 39 text

{content} {content} {content} {content} {content}
{content}
{content}
{content}

Slide 40

Slide 40 text

{content} {content} {content} {content} {content}
{content}
{content}
{content}

Slide 41

Slide 41 text

{content} {content} {content} {content} {content}
{content}
{content}
{content}

Slide 42

Slide 42 text

{content} {content} {content} {content} {content}
{content}
{content}
{content}

Slide 43

Slide 43 text

{content} {content} {content} {content} {content}
{content}
{content}
{content}

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

interface IProps extends ITextCSSProps { children: React.ReactNode; className?: string; as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'span' | 'strong' | 'p' | 'div' | 'i' | 'legend' | 'details' | 'footer' | 'summary'; id?: string; onClick?: React.MouseEventHandler; format?: boolean; noOfLines?: INoOfLines; center?: boolean; target?: '_blank' | '_self'; 'data-test'?: string; 'aria-label'?: string; ellipsis?: boolean; } export default function Text({ children, className, as, id, color = 'darker-grey', fontSize = 16, fontWeight = 400, format = false, center = false, noOfLines, 'data-test': dataTest, onClick, ellipsis, ...otherProps }: IProps) { const finalClassName = classNames( dreamCSSClasses({ ...otherProps, color, fontSize, fontWeight, noOfLines }), className, format && styles.format, center && styles.center, ellipsis && styles.ellipsis, ); const Component = as ?? 'div'; return ( {children} ); }

Slide 47

Slide 47 text

dreamCSSClasses(props)

Slide 48

Slide 48 text

export function deprecatedDreamCSSClasses(props: IDreamCSS) { let classes: string = ''; const propNames = Object.keys(props); for (const propName of propNames) { const propValue = props[propName]; if (!ALLOWED_PROPS[propName]) { continue; } if (typeof propValue === 'object') { classes = classesForObject(classes, propName, propValue); } else { classes = classesForProps(classes, propName, propValue); } } return classes.trim(); } function classesForObject(classes: string, prop: string, props: any) { if (isNotSet(props.widescreen) && props.desktop) { props.widescreen = props.desktop; }

Slide 49

Slide 49 text

function classesForObject(classes: string, prop: string, props: any) { if (isNotSet(props.widescreen) && props.desktop) { props.widescreen = props.desktop; } if (isNotSet(props.tablet) && props.mobile) { props.tablet = props.mobile; } const modifiers = Object.keys(props); for (const modifier of modifiers) { const propValue = props[modifier]; if (modifier === 'l') { classes = classesForProps(classes, prop, propValue); } else { classes = classesForProps(classes, `${prop}-${modifier}`, propValue); } } return classes; } function classesForProps(classes: string, propName: string, propValue: any) { if (typeof propValue === 'boolean' && propValue) { classes += `${propName}-default `; } else {

Slide 50

Slide 50 text

const modifiers = Object.keys(props); for (const modifier of modifiers) { const propValue = props[modifier]; if (modifier === 'l') { classes = classesForProps(classes, prop, propValue); } else { classes = classesForProps(classes, `${prop}-${modifier}`, propValue); } } return classes; } function classesForProps(classes: string, propName: string, propValue: any) { if (typeof propValue === 'boolean' && propValue) { classes += `${propName}-default `; } else { classes += `${propName}-${propValue} `; } return classes; } function isNotSet(value) { return typeof value === 'undefined' || value === null; }

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

! Problem #1 - CSS bundle size All possible class names must be pre-generated and included in the CSS bundle because they are generated dynamically.

Slide 54

Slide 54 text

! Problem #1 - CSS bundle size gap: 14 sizes * 5 variants = 46 margin: 7 props * 14 sizes * 5 variants = 350 padding: 7 props * 14 sizes * 5 variants = 350 colors: 2 props * 12 colors * 5 variants = 120 ... ... total: ~2000 class names !

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

! Problem #2 - too many loops dreamCSSClasses was called for many components to build their class names. This results in loops and variable generation, resulting in many GCs and slowing rendering. Especially the initial rendering.

Slide 57

Slide 57 text

! Problem #2 - too many loops dreamCSSClasses was called for many components to build their class names. This results in loops and variable generation, resulting in many GCs and slowing rendering. Especially the initial rendering.

Slide 58

Slide 58 text

! Problem #2 - too many loops

Slide 59

Slide 59 text

! Problem #2 - too many loops 2000+ calls to dreamCSSClasses for single render

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

! Problem #3 - too many props Core components like Text and Flex, accepted subsets of dreamCSSClasses props, leading to overcomplicated code.

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

interface IProps extends ITextCSSProps { children: React.ReactNode; className?: string; as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'span' | 'strong' | 'p' | 'div' | 'i' | 'legend' | 'details' | 'footer' | 'summary'; id?: string; onClick?: React.MouseEventHandler; format?: boolean; noOfLines?: INoOfLines; center?: boolean; target?: '_blank' | '_self'; 'data-test'?: string; 'aria-label'?: string; ellipsis?: boolean; } export default function Text({ children, className, as, id, color = 'darker-grey', fontSize = 16, fontWeight = 400, format = false, center = false, noOfLines, 'data-test': dataTest, onClick, ellipsis, ...otherProps }: IProps) { const finalClassName = classNames( dreamCSSClasses({ ...otherProps, color, fontSize, fontWeight, noOfLines }), className, format && styles.format, center && styles.center, ellipsis && styles.ellipsis, ); const Component = as ?? 'div'; return ( {children} ); }

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Project: Wakeup

Slide 68

Slide 68 text

Project: Wakeup

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

- Button and useOnClick(prop) - Form components - interface of dream components was consistent - developers were able to build really fast ! The good

Slide 71

Slide 71 text

- CSS bundle size - performance - spacing props - very complicated core components - core components accepted a lot of props - still needed custom css for borders/shadows - a lot of nesting of core components like Text / Flex ! The bad

Slide 72

Slide 72 text

- a lot of "as=" props complicating everything - Flex component wasn't flexible enough - Text component was too complex - designers were constrained - dream was desktop first ! The ugly

Slide 73

Slide 73 text

- a lot of "as=" props complicating everything - Flex component wasn't flexible enough - Text component was too complex - designers were constrained - dream was desktop first ! The ugly pun intended !

Slide 74

Slide 74 text

dreamCSSClasses(props)

Slide 75

Slide 75 text

dreamCSSClasses(props)

Slide 76

Slide 76 text

https://tips.rstankov.com/p/tips-for-tailwind-css

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Step 1: Unify Dream and Tailwind class names Step 2: Replace dreamCSSClasses with "tw" helper Step 3: Inline "Flex" and "Text" Step 4: Switch to Tailwind Step 5: Replace "tw" with "eslint-plugin-tailwindcss" ⚔ Battle plan "

Slide 79

Slide 79 text

Step 1: Unify Dream and Tailwind class names Step 2: Replace dreamCSSClasses with "tw" helper Step 3: Inline "Flex" and "Text" Step 4: Switch to Tailwind Step 5: Replace "tw" with "eslint-plugin-tailwindcss" ⚔ Battle plan "

Slide 80

Slide 80 text

{content} {content} {content} {content}

Slide 81

Slide 81 text

{content}
{content}
{content}
{content}

Slide 82

Slide 82 text

{content}
{content}
{content}
{content}
{content}
! "

Slide 83

Slide 83 text

Step 1: Unify Dream and Tailwind class names Step 2: Replace dreamCSSClasses with "tw" helper Step 3: Inline "Flex" and "Text" Step 4: Switch to Tailwind Step 5: Replace "tw" with "eslint-plugin-tailwindcss" ⚔ Battle plan "

Slide 84

Slide 84 text

! "

Slide 85

Slide 85 text

! "

Slide 86

Slide 86 text

! " // NOTE(rstankov): List only what is being used // - All properties can be found here - https://tailwindcss.com/docs/utility-first // - PurgeCSS text searches the app, so if a value is in this list it is included in bundle, however interpolation makes sure we don't include all colors type ITailwindClassNames = | 'antialiased' | 'border' | 'cursor-pointer' | `m${IDirection}-${IDistanceWithAuto}` | `w-${IDistanceWithFull}` // ... list all classNames used in the system type IColors = 'brand' | 'gray-50' | 'gray-400' | 'gray-900' | 'white'; type IDirection = 'x' | 'y' | 't' | 'r' | 'b' | 'l'; type IDistance = 0 | 1 | '1.5' | 2 | 4 | 5 | 6 | 8 | 12 | 16 | 40; type IDistanceWithFull = 'full' | IDistance | '3xl'; type IDistanceWithAuto = 'auto' | IDistance; type ISize = 'lg' | 'xl' | '4xl'; type IBreakpoints = 'xs' | 'sm' | 'md' | 'lg' | 'xl'; // NOTE(rstankov): Check if strings contains only Tailwind classes // Explanation how this works: // - https://www.kirillvasiltsov.com/writing/type-check-tailwind-css/ export type ITailwind = S extends `${infer Class} ${infer Rest}` ? Class extends ITailwindClassNames ? `${Class} ${ITailwind}` : never : S extends `${infer Class}` ? Class extends ITailwindClassNames ? S : never : never; // NOTE(rstankov): Helper to verify class names listed match available Tailwind class names export default function tw(classes: ITailwind) { return classes; }

Slide 87

Slide 87 text

! " https://jscodeshift.com/

Slide 88

Slide 88 text

import { Button, Text, dm } from 'ph/components/dream'; import { Text, dm } from 'ph/components/dream'; import Button from 'ph/components/dream/Button'; jscodeshift -t transform.js components/*.tsx

Slide 89

Slide 89 text

module.exports = function(fileInfo, api, options) { const j = api.jscodeshift; const root = j(fileInfo.source); // Find import declarations from 'ph/components/dream' root.find(j.ImportDeclaration, { source: { type: 'Literal', value: 'ph/components/dream', }, }) .forEach(path => { let buttonImportSpecifier = path.value.specifiers.find( specifier => specifier.imported && specifier.imported.name === 'Button' ); // If Button is imported from the package, modify the import statement if (buttonImportSpecifier) { // Remove Button from the original import path.value.specifiers = path.value.specifiers.filter(specifier => specifier.imported.name !== 'Button'); // If after removing Button there are no specifiers left, remove the whole import statement if (path.value.specifiers.length === 0) { j(path).remove(); } // Create a new import declaration for Button const newImportDeclaration = j.importDeclaration( [j.importDefaultSpecifier(j.identifier('Button'))], j.literal('ph/components/dream/Button') ); // Insert the new import declaration after the original one j(path).insertAfter(newImportDeclaration); } }); return root.toSource({quote: 'single'}); };

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

! "

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

! May flow "

Slide 95

Slide 95 text

- add starting sample - provide a lot of examples - negative statement (like DON'T) don't work mostly - start new chat sessions often, LLMs are autoregressive - it will get you to 80% and will get stuck - stop using it when stuck ! Prompting Tips "

Slide 96

Slide 96 text

Step 1: Unify Dream and Tailwind class names Step 2: Replace dreamCSSClasses with "tw" helper Step 3: Inline "Flex" and "Text" Step 4: Switch to Tailwind Step 5: Replace "tw" with "eslint-plugin-tailwindcss" ⚔ Battle plan "

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

April 2 ! (intentional)

Slide 99

Slide 99 text

Recap

Slide 100

Slide 100 text

https://tips.rstankov.com