Practical examples। Live demos।
कोई example चुनिए और फ़ुल-स्क्रीन में खोलिए: live preview और source code साथ-साथ।
Basics
4हर Nuclo app इन्हीं patterns से बनता है।
Counter
state बदलिए, फिर update() call कीजिए। dynamic text अपने-आप update हो जाता है।
Todo list
repeat होने वाले items के लिए list() इस्तेमाल कीजिए। हर change के बाद update() call कीजिए।
Search filter
सादे JavaScript से array filter कीजिए और list() से result render कीजिए।
Styling
class बनाने के लिए css() और छोटे conditional बदलावों के लिए cx() इस्तेमाल कीजिए।
Components
4Static और dynamic params, children, identity से match होने वाली lists और nested data, सब plain functions से। हर preview अपने mounts गिनता है।
Static और dynamic params
value वाला param component बनते समय बस एक बार पढ़ा जाता है। function वाला param हर update() पर दोबारा पढ़ा जाता है। दोनों ही case में component खुद सिर्फ़ एक बार चलता है, जिसे mount counter से verify कर सकते हैं।
Child nodes और modifiers
component spread करके अपने children आगे पास करता है: builders, text, attribute objects और modifiers। conditional हिस्से when() के अंदर रहते हैं, इसलिए उन्हें wrap करने वाला component दोबारा नहीं बनता।
List में object identity
list() किसी row को तब तक रखता है जब तक उसका item array में है। input या focus खोए बिना reorder, add और rename कीजिए। switch दिखाता है कि हर update पर objects clone करने से identity टूटने की कीमत क्या है।
Nested data
members और उनके tasks वाली team: हर level पर एक component, nested list() calls और उन्हीं objects से function के ज़रिए निकाले गए counters। deep changes सिर्फ़ अपने nodes को patch करते हैं।
API
13हर building block के लिए एक छोटा demo, वही जो docs में भी दिखते हैं।
Tag builders
builder में attributes, static children और dynamic text पास होते हैं। Rename state बदलकर update() call करता है।
Components
component बस एक सादा function है। हर Counter() call का अपना अलग counter होता है।
Attributes
attribute की value function भी हो सकती है। दूसरा बटन busy बदलकर update() call करता है, और Save के disabled और aria-busy उसी के हिसाब से बदल जाते हैं।
Events
onInput और onKeyDown आम attributes ही हैं। कुछ टाइप कीजिए और clear करने के लिए Esc key दबाइए।
Lifecycle
onMount interval शुरू करता है और उसका cleanup function return करता है। unmount होते ही cleanup चलता है, इसलिए दोबारा mount होने तक timer की गिनती रुकी रहती है।
Explicit updates
count++ सिर्फ़ variable बदलता है। text तब update होता है जब इस बटन या पेज की किसी और activity से अगला update() चलता है।
Dynamic values
नाम बदलिए। static text element बनते समय ही तय हो गया था, जबकि dynamic text हर update() पर दोबारा चलता है। forceUpdate() पूरा tree दोबारा बनाता है, इसलिए static text भी update हो जाता है।
update()
हर update() सारी dynamic values चला देता है, चाहे उनका data बदला हो या नहीं। इस पेज के बाकी demos भी counter बढ़ाते हैं।
scope()
दोनों बटन cart में item जोड़ते हैं और user का नाम बदलते हैं। update('cart') सिर्फ़ उसी scope का panel update करता है।
when()
पहली true condition चुनी जाती है, और कोई भी true न हो तो .else() render होता है। नई branch शुरू से बनती है।
list()
rows object identity से match होती हैं। किसी row पर check लगाइए, फिर Reverse दबाइए: checkbox समेत वही <li> अपनी जगह से खिसकता है।
region() और into()
stacked main region और side region वाला layout बाहर से भरा जाता है। Home में कुछ लिखिए, dialog खोलकर बंद कीजिए: text वैसा ही रहता है। Article एक ही into() से दोनों regions भर देता है।
forceUpdate()
update() सिर्फ़ dynamic lang वाली line update करता है। forceUpdate() App को दोबारा call करके उसी <h1> को patch करता है। यानी explicit form सिर्फ़ इसी tree को दोबारा बनाता है, पूरे पेज को नहीं।
Styling
3Class बनाइए, classes compose कीजिए और एक छोटी theme इस्तेमाल कीजिए।
Forms
3State पढ़ने वाले सादे functions से validation, touched fields, steps और dynamic rows।
Field validation
state पढ़ने वाले functions के रूप में लिखे rules, field छूने के बाद दिखने वाले errors, pending state वाला debounced async check, और submit जो इन सबके पूरा होने का wait करता है।
Multi-step wizard
एक data object पर when() की branches के रूप में तीन steps। Next मौजूदा step के fields को touched mark करता है और validation पास होने पर ही आगे बढ़ता है। Review में सारी values Edit link के साथ दिखती हैं।
Dynamic rows
array में रखी invoice की rows list() से render होती हैं। हर row अपनी validation खुद करती है और total में सिर्फ़ valid rows गिनी जाती हैं। बाकी rows add या remove करने पर मौजूदा rows के inputs वैसे ही बने रहते हैं।
SVG
27SVG tag builders से बने charts, animations, tools और games, जो plain data, pointer events और update() से चलते हैं।
Bar chart
array से बने rect। उनके y और height data के functions हैं, इसलिए update() bars को खिसका देता है।
Line chart
points से बना d वाला एक path, साथ में gradient से भरा area और हर point के लिए एक circle।
Donut chart
हर slice के लिए एक circle। stroke-dasharray उसे ring में उसका हिस्सा देता है और stroke-dashoffset तय करता है कि वह कहाँ से शुरू होगा।
SVG animation
SMIL के builders: animateSvg सूरज को धड़कता हुआ effect देता है और animateTransformSvg ग्रह को उसकी orbit में घुमाता है। dynamic dur speed तय करता है।
Gauge
stroke-dashoffset से भरता arc और CSS transform से घूमती सुई। दोनों एक ही number के functions हैं।
Radar chart
पाँच axes पर पाँच values। shape एक path है जिसका d data से calculate होता है, इसलिए update() उसका आकार बदल देता है।
Heatmap
rect का grid, जिनका fill-opacity एक level के हिसाब से बदलता है। level बढ़ाने के लिए cell पर click कीजिए। titleSvg child उसका tooltip है।
Hover tooltip
SVG elements पर pointer events। onPointerEnter active point चुनता है और tooltip का group खिसककर उसके पास पहुँच जाता है।
Curve editor
cubic Bézier curve के handles drag कीजिए। onPointerDown point चुनता है, svg का onPointerMove उसे खिसकाता है और path उसी के हिसाब से बदलता है।
घड़ी
सुइयाँ lines हैं जिनका transform समय का function है। onMount घड़ी शुरू करता है और cleanup return करता है।
Icon toggles
CSS transition से cross में बदलता menu icon, और click पर शुरू होने वाले SMIL animation से उभरता दिल।
Live stream
timer पर दो data series बाईं ओर scroll होती हैं। एक tick जितनी duration का CSS transition motion को smooth बना देता है। किसी sample की value देखने के लिए hover कीजिए।
Candlestick chart
चालीस slots अपनी-अपनी candle का data पढ़ते हैं। timeframe बदलिए, average दिखाइए या छुपाइए, values देखने के लिए hover कीजिए, या live candle को update होते देखिए।
Bar chart race
दस bars हमेशा DOM में रहते हैं। उनकी rank translateY तय करती है, इसलिए values बदलने पर transition हर bar को उसकी row तक खिसका देता है।
Stacked area chart
चार stacked bands, stream और percent layout के बीच अपना आकार बदलते हैं। series छुपाने पर उसके band की thickness बस zero हो जाती है, इसलिए हर path के commands का structure वही रहता है।
Treemap
square जैसे boxes वाला layout जिसमें groups खोलकर अंदर जा सकते हैं। metric बदलने पर हर cell अपनी नई जगह पर खिसक जाता है, और group पर click करने पर उसके children दिखने लगते हैं।
Live histogram
चार distributions से लगातार samples आते रहते हैं। हर बदलाव पर bin counts दोबारा निकलते हैं, fitted bell curve mean के हिसाब से बदलता है और slider bins की संख्या तय करता है।
Drawing pad
pointer events से polyline बनाइए। list() हर stroke के लिए एक polyline render करता है। जो stroke बन रहा है वह हर update() पर लंबा होता जाता है।
Rotary knob
custom slider: pointer का angle value तय करता है, arrow keys उसे एक-एक step बदलती हैं और pathLength="100" value को stroke की लंबाई में बदल देता है।
Color wheel
angle से hue और center से दूरी के हिसाब से saturation चुनिए। नब्बे wedges, एक radial gradient और एक dark disc मिलकर wheel बनाते हैं।
Spirograph
पहिया ring के अंदर घूमता है और pen उसका path बनाता है। हर frame एक stroke को थोड़ा लंबा करता है और सिर्फ़ अपना scope() update करता है।
उछलती गेंदें
requestAnimationFrame loop में gravity, walls और collisions। गेंद उछालने के लिए पीछे खींचकर छोड़ दीजिए।
Gooey blobs
blur और color matrix filter से blobs आपस में घुल जाते हैं। circles की लड़ी pointer के पीछे-पीछे चलती है।
Force graph
nodes एक-दूसरे को धकेलते हैं, links खींचते हैं और layout धीरे-धीरे शांत होकर थम जाता है। nodes drag कीजिए, या नया child जोड़ने के लिए उस पर click कीजिए।
Brush और zoom
ऊपर वाले chart को zoom करने के लिए overview में एक range चुनिए। brush के हिसाब से बदलने वाला clipPath चुने हुए हिस्से को रंग देता है।
टिक-टैक-टो
computer के साथ खेलिए। when() हर mark जोड़ता है और उसके stroke का CSS animation उसे बनते हुए दिखाता है।
Circular text
badge के लिए text लिखिए। textPath उसे circle पर मोड़ता है और textLength उसे ठीक एक चक्कर में fit कर देता है।
Routing
6Route tables, matching, active route, layers और loaders। nuclo-router एक अलग package है: npm install nuclo nuclo-router.
Routing वाला app
frame के अंदर अपने router के साथ चलता app: route table, एक outlet(), सादे links, pending state और 404। links खोलिए, फिर browser का back button इस्तेमाल कीजिए।
Nested routes
Docs एक layout है जिसके child routes हैं। topic उसके outlet() में render होता है और ./topic links layout के route के relative resolve होते हैं। notes लिखिए, फिर topic बदलिए: layout दोबारा नहीं बनता।
Route loader
post page एक load() export करता है जो page बनने से पहले चलता है। wait के दौरान router.pending देखिए। loader किसी टूटी post पर throw करे, तो Retry के साथ error state देखिए।
Layers
order form customer का page layer की तरह push करता है और उसके result का wait करता है। form दोबारा render नहीं होता, इसलिए उसके notes बचे रहते हैं। Save push को resolve करता है, और Cancel या back button उसे dismiss कर देता है।
कई routers और loading
दो routers, दो spinners। shop के layout और उसके section, दोनों में load() हैं, और दोनों एक ही router.pending के नीचे parallel चलते हैं। sidebar का अलग router बनता है लेकिन start नहीं होता। उसका अपना loading flag onNavigate से refresh होता है।
Pattern matching
static, :param और *rest patterns, साथ में दो parents के नीचे लगी एक section table। path लिखकर match() देखिए: यह कुछ भी load किए बिना match करता है।
Apps
15बड़े फ़ुल-स्क्रीन programs, tabs में उनके असली source के साथ: windows वाला desktop, data grid, chat, games, editors और ऐसी libraries जो अपना state खुद संभालती हैं।
Page builder
सीधे page पर लिखिए। blocks का order बदलने या उन्हें layout में ले जाने के लिए grip drag कीजिए। नया block जोड़ने के लिए / दबाइए और actions के लिए right-click कीजिए। grid और flex layouts को style कीजिए, changes undo कीजिए, phone size में preview देखिए और HTML export कीजिए।
Algorithm stepper
engine सादा JavaScript है: generators array बदलते हैं और हर step के बाद yield करते हैं। player next() और update() call करता है। bars उन्हीं objects पर एक list() हैं, इसलिए swap सिर्फ़ दो elements को खिसकाता है।
Kanban board
team का board: tasks जोड़िए, लोगों को assign कीजिए, dialog में edit कीजिए, person या search से filter कीजिए, columns के बीच drag कीजिए और delete को undo कीजिए। columns और cards उन्हीं objects के सादे arrays हैं, और हर move एक splice() है। rows identity से match होती हैं, इसलिए card को drag, sort, assign या edit करने पर उसका element reuse होता है। screen पर mount count इसे verify करता है।
three.js inspector
ऐसी library जिसका अपना state है। panel position, color और flags सीधे three.js objects से पढ़ता है और edits भी उन्हीं पर वापस लिखता है। render loop हर frame में एक बार update() call करता है, और state की कोई अलग copy नहीं रखी जाती।
Infinite scroll
बिना अंत वाला feed: तेज़ fake fetch() हर नया page बनाकर एक सादे array में जोड़ता है। DOM में सिर्फ़ viewport के पास की rows रहती हैं: list() दो spacers के बीच posts.slice(start, end) render करता है, इसलिए एक लाख posts पर भी page हल्का रहता है। असली अंत पर लगा sentinel दिखने से पहले ही अगला page माँग लेता है।
Desktop
files, notes, terminal, calculator, timer, kanban board, finance app और calendar वाला चलता-फिरता desktop। browser में save होने वाली shared simulated disk पर folders बनाइए और organize कीजिए, notes edit कीजिए और shell commands चलाइए। calculator में सीधे keyboard से टाइप कीजिए, input पर focus करने के लिए terminal में कहीं भी click कीजिए, board पर columns के बीच cards drag कीजिए, या finance spreadsheet में हिसाब रखिए और chart बदलते देखिए। हिसाब IndexedDB में रहता है। app की windows खिसकाने, resize करने या app बदलने पर उनका state बना रहता है। चलता हुआ timer into() से taskbar tray भरता है।
Node graph
numbers, text और switches को math, comparison और If nodes से जोड़िए। टाइप करते ही हर value update होती है। functions भी graphs हैं: उनके Input और Output nodes हर Call के ports हैं। Call अपने ही function को भी call कर सकता है, इसलिए factorial और sum recursion से चलते हैं और If रुकने की condition देता है। engine सादा JavaScript है: हर node एक generator है जो input माँगने के लिए yield करता है। एक loop अपने stack से उन्हें चलाता है, इसलिए sum(50000) JavaScript call stack इस्तेमाल किए बिना लगभग एक सेकंड के दसवें हिस्से में पचास हज़ार calls की depth तक पहुँच जाता है। रुकने की condition न हो, तो recursion को सिर्फ़ steps की limit रोकती है। drag करने पर x और y बदलते हैं और सिर्फ़ canvas scope update होता है। run count दिखाता है कि node खिसकाने से graph नहीं चलता।
Data grid
10,000 सादे row objects पर बनी एक पूरी तरह काम करने वाली table। search, filter, sort और paging array पर सादे functions हैं। table body मौजूदा page पर एक list() है। identity से match होने की वजह से sorting page पर बची rows को खिसकाती है, और edit की हुई row हर जगह वही object रहती है। बनी हुई rows की गिनती इसे verify करती है।
Chat
तीन channels वाली chat। हर channel एक सादा object है: history, cursor, typing state, draft और scroll position। channel बदलने पर सब जस का तस रहता है, और update("chat-sidebar") सिर्फ़ unread messages के badges refresh करता है। unshift() से पुरानी history ऊपर load होती रहती है और scrollTop उतना बढ़ता है जितनी height जुड़ी। message server तक पहुँचने से पहले ही दिख जाता है। send() उसी object को patch करता है, और Retry, Edit, Delete और reactions उसकी row दोबारा नहीं बनाते। messages में code, bold text, links और @mentions दिखते हैं।
Live dashboard
cards अलग-अलग rate पर refresh होते हैं: 20 बार प्रति सेकंड से लेकर एक बार तक। हर card एक scope() है और उसका timer उसी card की id के साथ update() call करता है, इसलिए हर tick सिर्फ़ उसी card की values चलाता है। screen पर हर dynamic value के चलने की गिनती दिखती है। सादे update() पर switch कीजिए: घड़ी की value एक बार बदलने के लिए प्रति सेकंड लगभग सौ बार calculation होती है।
Pixel editor
image रंग बताने वाली 1024 strings का एक सादा array है। हर cell उसी से अपना रंग पढ़ता है। stroke array बदलता है और update() call करता है। stroke पूरा होने पर editor slice() से एक copy रख लेता है। undo, redo और history (वह भी एक list() है) का हर step पुराना array वापस रख देता है: न store, न immutable data।
Snake
game loop एक timer है जो सादे objects पर step() और हर tick पर एक बार update() call करता है। साँप अपने cell objects की list() है। हर tick पर पूँछ का object नया सिर बन जाता है, इसलिए एक element खिसकता है और नया नहीं बनता। बने segments की गिनती साँप के खाना खाने पर ही बढ़ती है। खाना keyframes() animation से उभरता है।
Step sequencer
अपनी घड़ी वाली library। Web Audio अपनी timeline पर हर note पहले से schedule करता है। pattern booleans का सादा 2D array है, जिसे grid toggle करता है और scheduler पढ़ता है, इसलिए click का असर अगली बार सुनाई देता है। जो step सुनाई दे रहा है वह बदलने पर playhead सिर्फ़ अपना scope() update करता है। पहली बार Play दबाने पर audio शुरू होता है और mount cleanup में बंद होता है, इसलिए example छोड़ते ही आवाज़ रुक जाती है।
Locale switcher
एक भाषा वाले app की तरह हर label एक static value है, इसलिए update() उसे दोबारा नहीं पढ़ता। cart पर click करने से सिर्फ़ quantity और total update होते हैं। भाषा बदलने पर इस tree का forceUpdate() चलता है, जो components दोबारा चलाकर उन्हीं elements को patch करता है, lang और dir समेत। currency, plurals, dates और lists Intl से format होते हैं। भाषा बदलने पर cart और टाइप किया हुआ coupon बने रहते हैं।
Theme editor
छोटी component library: createCss() tokens को नाम देता है और variants() उनसे buttons, badges, cards, inputs, alerts और switches बनाता है। tokens CSS variables हैं, इसलिए हर class एक बार compile होती है। हर control एक सादा theme object बदलता है और update() call करता है, जो एक style element का text दोबारा लिख देता है।