Spaces:
Running
Running
File size: 5,003 Bytes
e9674a6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
interface FeatureAPI {
title: string
description: string
pattern: string
}
const getPromptFromFeatures = (feats: FeatureAPI[]) =>
feats.map(({ title, description, pattern }) => `## "${title}": ${description}.\nExample: \`${pattern}\``).join("\n")
export const attributes: FeatureAPI[] = [
{
title: "x-data",
description: "Declare a new Alpine component and its data for a block of HTML",
pattern:
`<div x-data="{ open: false }">
...
</div>`
},
{
title: "x-bind",
description: "Dynamically set HTML attributes on an element",
pattern:
`<div x-bind:class="! open ? 'hidden' : ''">
...
</div>`
},
{
title: "x-on",
description: "Listen for browser events on an element",
pattern:
`<button x-on:click="open = ! open">
Toggle
</button>`
},
{
title: "x-text",
description: "Set the text content of an element",
pattern:
`<div>
Copyright ©
<span x-text="new Date().getFullYear()"></span>
</div>`
},
{
title: "x-html",
description: "Set the inner HTML of an element",
pattern:
`<div x-html="(await axios.get('/some/html/partial')).data">
...
</div>`
},
{
title: "x-model",
description: "Synchronize a piece of data with an input element",
pattern:
`<div x-data="{ search: '' }">
<input type="text" x-model="search">
Searching for: <span x-text="search"></span>
</div>`
},
{
title: "x-show",
description: "Toggle the visibility of an element",
pattern:
`<div x-show="open">
...
</div>`
},
{
title: "x-transition",
description: "Transition an element in and out using CSS transitions",
pattern:
`<div x-show="open" x-transition>
...
</div>`
},
{
title: "x-for",
description: "Repeat a block of HTML based on a data set",
pattern:
`<template x-for="post in posts">
<h2 x-text="post.title"></h2>
</template>`
},
{
title: "x-if",
description: "Conditionally add/remove a block of HTML from the page entirely",
pattern:
`<template x-if="open">
<div>...</div>
</template>`
},
{
title: "x-init",
description: "Run code when an element is initialized by Alpine",
pattern:
`<div x-init="date = new Date()"></div>`
},
{
title: "x-effect",
description: "Execute a script each time one of its dependencies change",
pattern:
`<div x-effect="console.log('Count is '+count)"></div>`
},
{
title: "x-ref",
description: "Reference elements directly by their specified keys using the $refs magic property",
pattern:
`<input type="text" x-ref="content">
<button x-on:click="navigator.clipboard.writeText($refs.content.value)">
Copy
</button>`
},
{
title: "x-cloak",
description: "Hide a block of HTML until after Alpine is finished initializing its contents",
pattern:
`<div x-cloak>
...
</div>`
},
{
title: "x-ignore",
description: "Prevent a block of HTML from being initialized by Alpine",
pattern:
`<div x-ignore>
...
</div>`
},
]
export const attributesPrompt = getPromptFromFeatures(attributes)
export const properties: FeatureAPI[] = [
{
title: "$store",
description: "Access a global store registered using Alpine.store(...)",
pattern: `<h1 x-text="$store.site.title"></h1>`
},
{
title: "$el",
description: "Reference the current DOM element",
pattern:`<div x-init="new Pikaday($el)"></div>`
},
{
title: "$dispatch",
description: "Dispatch a custom browser event from the current element",
pattern:
`<div x-on:notify="...">
<button x-on:click="$dispatch('notify')">...</button>
</div>`
},
{
title: "$watch",
description: "Watch a piece of data and run the provided callback anytime it changes",
pattern:
`<div x-init="$watch('count', value => {
console.log('count is ' + value)
})">...</div>`
},
{
title: "$refs",
description: "Reference an element by key (specified using x-ref)",
pattern:
`<div x-init="$refs.button.remove()">
<button x-ref="button">Remove Me</button>
</div>`
},
{
title: "$nextTick",
description: "Wait until the next \"tick\" (browser paint) to run a bit of code",
pattern:
`<div
x-text="count"
x-text="$nextTick(() => {"
console.log('count is ' + $el.textContent)
})
>...</div>`
},
]
export const propertiesPrompt = getPromptFromFeatures(properties)
export const methods: FeatureAPI[] = [
{
title: "Alpine.data",
description: "Reuse a data object and reference it using x-data",
pattern:
`<div x-data="dropdown">
...
</div>`
},
{
title: "Alpine.store",
description: "Declare a piece of global, reactive, data that can be accessed from anywhere using $store",
pattern:
`<button @click="$store.notifications.notify('...')">
Notify
</button>
...
Alpine.store('notifications', {
items: [],
notify(message) {
this.items.push(message)
}
})`
},
]
export const methodsPrompt = getPromptFromFeatures(methods)
export const alpine = "# Alpine.js docs\n"+ attributesPrompt // + propertiesPrompt + methodsPrompt |