|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +'use client'; |
| 9 | + |
| 10 | +import { useState } from 'react'; |
| 11 | +import * as stylex from '@stylexjs/stylex'; |
| 12 | +import x from '@stylexjs/atoms'; |
| 13 | + |
| 14 | +// === Edge case: atoms assigned to variables === |
| 15 | +const redText = x.color.red; |
| 16 | +const boldText = x.fontWeight._700; |
| 17 | + |
| 18 | +// === Edge case: conditional variable assignment === |
| 19 | +const maybeStyle = true ? x.color.green : x.color.gray; |
| 20 | + |
| 21 | +// === Component accepting atoms via xstyle prop === |
| 22 | +function Card({ |
| 23 | + xstyle, |
| 24 | + children, |
| 25 | +}: { |
| 26 | + xstyle?: stylex.StyleXStyles; |
| 27 | + children: React.ReactNode; |
| 28 | +}) { |
| 29 | + return ( |
| 30 | + <div {...stylex.props(styles.card, xstyle)}> |
| 31 | + {children} |
| 32 | + </div> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +export default function AtomsDemo() { |
| 37 | + const [color, setColor] = useState('tomato'); |
| 38 | + const [count, setCount] = useState(0); |
| 39 | + |
| 40 | + return ( |
| 41 | + <div |
| 42 | + {...stylex.props( |
| 43 | + x.display.flex, |
| 44 | + x.flexDirection.column, |
| 45 | + x.alignItems.center, |
| 46 | + x.padding._32px, |
| 47 | + x.gap._24px, |
| 48 | + x.fontFamily['system-ui, sans-serif'], |
| 49 | + x.minHeight['100vh'], |
| 50 | + )} |
| 51 | + > |
| 52 | + <h1 {...stylex.props(x.fontSize._2rem, x.fontWeight._700)}> |
| 53 | + Atoms Test Page |
| 54 | + </h1> |
| 55 | + |
| 56 | + {/* 1. Static atoms inline */} |
| 57 | + <Card> |
| 58 | + <span {...stylex.props(x.color.green)}> |
| 59 | + 1. Inline static atom |
| 60 | + </span> |
| 61 | + </Card> |
| 62 | + |
| 63 | + {/* 2. Atoms as variables */} |
| 64 | + <Card> |
| 65 | + <span {...stylex.props(redText, boldText)}> |
| 66 | + 2. Variable atoms: red bold |
| 67 | + </span> |
| 68 | + </Card> |
| 69 | + |
| 70 | + {/* 3. Atoms through xstyle prop */} |
| 71 | + <Card xstyle={x.backgroundColor.lavender}> |
| 72 | + 3. xstyle prop: lavender bg |
| 73 | + </Card> |
| 74 | + |
| 75 | + {/* 4. Array of atoms through xstyle */} |
| 76 | + <Card xstyle={[x.backgroundColor.lightyellow, x.color.navy]}> |
| 77 | + 4. xstyle array: yellow bg + navy text |
| 78 | + </Card> |
| 79 | + |
| 80 | + {/* 5. Dynamic atoms */} |
| 81 | + <div |
| 82 | + {...stylex.props( |
| 83 | + x.padding._16px, |
| 84 | + x.color(color), |
| 85 | + x.borderWidth._2px, |
| 86 | + x.borderStyle.solid, |
| 87 | + x.borderColor(color), |
| 88 | + x.borderRadius._8px, |
| 89 | + )} |
| 90 | + > |
| 91 | + 5. Dynamic: {color} |
| 92 | + </div> |
| 93 | + <div {...stylex.props(x.display.flex, x.gap._8px)}> |
| 94 | + {['tomato', 'dodgerblue', 'purple', 'green'].map((c) => ( |
| 95 | + <button |
| 96 | + key={c} |
| 97 | + onClick={() => setColor(c)} |
| 98 | + {...stylex.props( |
| 99 | + x.padding._8px, |
| 100 | + x.borderRadius._4px, |
| 101 | + x.backgroundColor(c), |
| 102 | + x.color.white, |
| 103 | + x.borderStyle.none, |
| 104 | + x.cursor.pointer, |
| 105 | + )} |
| 106 | + > |
| 107 | + {c} |
| 108 | + </button> |
| 109 | + ))} |
| 110 | + </div> |
| 111 | + |
| 112 | + {/* 6. Atoms + stylex.create mixed */} |
| 113 | + <Card xstyle={x.color.darkgreen}> |
| 114 | + <span {...stylex.props(styles.emphasis)}> |
| 115 | + 6. stylex.create + atom xstyle |
| 116 | + </span> |
| 117 | + </Card> |
| 118 | + |
| 119 | + {/* 7. Deduplication: atoms override create */} |
| 120 | + <div |
| 121 | + {...stylex.props( |
| 122 | + styles.redText, |
| 123 | + x.color.blue, |
| 124 | + )} |
| 125 | + > |
| 126 | + 7. Dedup: should be blue (atom overrides create) |
| 127 | + </div> |
| 128 | + |
| 129 | + {/* 8. Computed key syntax */} |
| 130 | + <div |
| 131 | + {...stylex.props( |
| 132 | + x.width['calc(100% - 64px)'], |
| 133 | + x.maxWidth['600px'], |
| 134 | + x.padding._16px, |
| 135 | + x.backgroundColor['#e8e8ff'], |
| 136 | + x.borderRadius._8px, |
| 137 | + x.textAlign.center, |
| 138 | + x.borderWidth._2px, |
| 139 | + x.borderStyle.dashed, |
| 140 | + x.borderColor['#8888ff'], |
| 141 | + )} |
| 142 | + > |
| 143 | + 8. Computed keys: calc(100% - 64px) width, dashed purple border |
| 144 | + </div> |
| 145 | + |
| 146 | + {/* 9. Leading underscore values */} |
| 147 | + <div |
| 148 | + {...stylex.props( |
| 149 | + x.fontSize['1.5rem'], |
| 150 | + x.lineHeight._2, |
| 151 | + x.padding._16px, |
| 152 | + x.margin._8px, |
| 153 | + x.backgroundColor['#fff3e0'], |
| 154 | + x.borderRadius._8px, |
| 155 | + x.fontWeight._700, |
| 156 | + )} |
| 157 | + > |
| 158 | + 9. Leading underscores: 1.5rem font, bold, orange bg, 16px padding |
| 159 | + </div> |
| 160 | + |
| 161 | + {/* 10. Conditional with && */} |
| 162 | + <div |
| 163 | + {...stylex.props( |
| 164 | + x.padding._16px, |
| 165 | + count > 0 && x.color.green, |
| 166 | + count === 0 && x.color.gray, |
| 167 | + )} |
| 168 | + > |
| 169 | + 10. Conditional &&: count={count} ( |
| 170 | + <button onClick={() => setCount(count + 1)} {...stylex.props(x.cursor.pointer)}>+</button> |
| 171 | + <button onClick={() => setCount(Math.max(0, count - 1))} {...stylex.props(x.cursor.pointer)}>-</button> |
| 172 | + ) |
| 173 | + </div> |
| 174 | + |
| 175 | + {/* 11. Ternary conditional */} |
| 176 | + <div |
| 177 | + {...stylex.props( |
| 178 | + x.padding._16px, |
| 179 | + count % 2 === 0 ? x.backgroundColor.lightyellow : x.backgroundColor.lightblue, |
| 180 | + )} |
| 181 | + > |
| 182 | + 11. Ternary: {count % 2 === 0 ? 'yellow' : 'blue'} bg |
| 183 | + </div> |
| 184 | + |
| 185 | + {/* 12. Static + dynamic coexist */} |
| 186 | + <div |
| 187 | + {...stylex.props( |
| 188 | + x.display.flex, |
| 189 | + x.gap._8px, |
| 190 | + x.alignItems.center, |
| 191 | + x.color(color), |
| 192 | + )} |
| 193 | + > |
| 194 | + 12. Static + dynamic together |
| 195 | + </div> |
| 196 | + |
| 197 | + {/* 13. Multiple dynamic atoms */} |
| 198 | + <div |
| 199 | + {...stylex.props( |
| 200 | + x.color(color), |
| 201 | + x.backgroundColor(color === 'tomato' ? '#fff0f0' : '#f0f0ff'), |
| 202 | + x.padding._16px, |
| 203 | + x.borderRadius._8px, |
| 204 | + )} |
| 205 | + > |
| 206 | + 13. Multiple dynamic: color + bg |
| 207 | + </div> |
| 208 | + |
| 209 | + {/* 14. Shorthand property */} |
| 210 | + <div |
| 211 | + {...stylex.props( |
| 212 | + x.margin._16px, |
| 213 | + x.padding._8px, |
| 214 | + x.borderWidth._1px, |
| 215 | + x.borderStyle.solid, |
| 216 | + x.borderColor.gray, |
| 217 | + )} |
| 218 | + > |
| 219 | + 14. Shorthand: margin, padding, border |
| 220 | + </div> |
| 221 | + |
| 222 | + {/* 15. Conditional variable */} |
| 223 | + <div {...stylex.props(maybeStyle, x.padding._8px)}> |
| 224 | + 15. Conditional variable (should be green) |
| 225 | + </div> |
| 226 | + |
| 227 | + {/* 16. Raw inline object via stylex.props (runtime inline style) */} |
| 228 | + {/* @ts-ignore */} |
| 229 | + <div {...stylex.props({color: 'blue', fontWeight: 'bold', padding: '16px', backgroundColor: '#e0e0ff', borderRadius: '8px'})}> |
| 230 | + 16. Raw inline object (runtime inline style, not atomic class) |
| 231 | + </div> |
| 232 | + |
| 233 | + {/* 17. Raw object mixed with atoms */} |
| 234 | + {/* @ts-ignore */} |
| 235 | + <div {...stylex.props(x.display.flex, x.gap._8px, {color: 'purple', fontStyle: 'italic'})}> |
| 236 | + 17. Atoms + raw object mixed |
| 237 | + </div> |
| 238 | + |
| 239 | + {/* 18. Raw object mixed with stylex.create */} |
| 240 | + {/* @ts-ignore */} |
| 241 | + <div {...stylex.props(styles.card, {color: 'darkred', fontWeight: 'bold'})}> |
| 242 | + 18. stylex.create + raw object mixed |
| 243 | + </div> |
| 244 | + </div> |
| 245 | + ); |
| 246 | +} |
| 247 | + |
| 248 | +const styles = stylex.create({ |
| 249 | + card: { |
| 250 | + width: '100%', |
| 251 | + maxWidth: 480, |
| 252 | + padding: '16px', |
| 253 | + borderColor: '#ddd', |
| 254 | + borderStyle: 'solid', |
| 255 | + borderWidth: 1, |
| 256 | + borderRadius: '8px', |
| 257 | + }, |
| 258 | + emphasis: { |
| 259 | + fontStyle: 'italic', |
| 260 | + fontWeight: 600, |
| 261 | + }, |
| 262 | + redText: { |
| 263 | + color: 'red', |
| 264 | + }, |
| 265 | +}); |
0 commit comments