CWA 2 is a prerelease (2.0.0-alpha), heading for a beta. It runs production sites; configuration can still change between releases, always with upgrade notes. View versions and support
Guides

Alternative UI Variants

Give the same component more than one look — with styles for CSS differences, or a ui/ Vue file for different markup.

The same component data can render in more than one way. Your Quote might be a plain pull quote, a boxed one, or a card with a big quotation mark, all from the same text and author fields. An editor picks the look in the manager panel's UI tab.

There are two approaches. Use styles for looks that differ only in CSS classes. Use a UI variant for a look that needs different markup.


Approach 1: Styles

Add styles to the Quote's defineCwaConfig in app/app/cwa/components/Quote/Quote.vue:

<script setup lang="ts">
import type { IriProp } from '#cwa/composables/cwa-resource'

defineCwaConfig({
  name: 'Quote',
  description: 'A pull quote with an optional author',
  icon: 'quote',
  group: 'Text',
  styles: {    boxed: { label: 'Boxed', classes: 'rounded bg-stone-100 py-4' },    centred: { label: 'Centred', classes: 'text-center' },  },})

const props = defineProps<IriProp>()
const { resource, exposeMeta } = useCwaComponent(props)
defineExpose(exposeMeta)
</script>

The template stays as it was in Your First Component.

How it works

styles — each style has a key (boxed), a label that editors see in the Style select, and the classes it applies. Don't add a "Default" style yourself: the select already offers Default, which applies none.

The classes are applied for you to the component's root element, here the <figure>. You don't need a :class binding.

One style at a time — editors pick one style unless you add allowMultiple: true to defineCwaConfig. Then the Style select lets them combine several, such as Boxed and Centred.

The key is what's saved. Picking Boxed stores style:boxed in the Quote's uiClassNames, never the classes. Choose keys you won't need to rename. You can change a label or its classes whenever you like, and every Quote using that style changes on the next deploy.

VIDEO: Selecting a Quote in edit mode, opening the UI tab, switching the Style select from Default to Boxed and then Centred, with the quote restyling on the page each time, then back to Default

Changing markup with a style

For a small change that classes can't make, such as showing an extra element, read the selected keys with styleKeys:

<template>
  <figure class="my-6 border-l-4 border-stone-400 pl-4">
    <span v-if="styleKeys.includes('boxed')" aria-hidden="true" class="text-6xl">“</span>    <blockquote class="text-xl italic">
      {{ resource?.data?.text || 'No quote yet' }}
    </blockquote>
    <figcaption
      v-if="resource?.data?.author"
      class="mt-2 text-sm"
    >
      — {{ resource.data.author }}
    </figcaption>
  </figure>
</template>

<script setup lang="ts">
import type { IriProp } from '#cwa/composables/cwa-resource'

// defineCwaConfig({ ... }) as above

const props = defineProps<IriProp>()
const { resource, exposeMeta, styleKeys } = useCwaComponent(props) defineExpose(exposeMeta)
</script>

styleKeys lists the keys stored on the component, in the order they were saved, and updates when an editor changes the style.

Don't use a style to render a different structure. If a style would show different elements, different child components or different semantic markup, make a UI variant instead. A style is meant to be combined and switched freely; a structure that depends on it is easy to break.

Approach 2: UI Variants (ui/ folder)

When a look needs different markup, not just different classes, write another Vue file in the component's ui/ folder.

app/app/cwa/components/Quote/
VQuote.vuedefault template
ui/
VCard.vueUI variant
admin/
VQuote.vueadmin tab

The ui/ file

Create app/app/cwa/components/Quote/ui/Card.vue:

<template>
  <div class="my-6 rounded-lg bg-stone-900 p-8 text-white">
    <span aria-hidden="true" class="block text-6xl leading-none">“</span>
    <p class="text-2xl">
      {{ resource?.data?.text || 'No quote yet' }}
    </p>
    <p v-if="resource?.data?.author" class="mt-4 text-sm uppercase tracking-wide">
      {{ resource.data.author }}
    </p>
  </div>
</template>

<script setup lang="ts">
import type { IriProp } from '#cwa/composables/cwa-resource'

defineCwaConfig({  name: 'Card',}) 
const props = defineProps<IriProp>()
const { resource, exposeMeta } = useCwaComponent(props)
defineExpose(exposeMeta)
</script>

There's nothing to register. The module finds ui/Card.vue from where it is and offers it in the UI select of the Quote's UI tab. When an editor picks it, the Quote renders through this file instead of Quote.vue, and its uiComponent stores the variant's full name, CwaComponentQuoteUiCard.

defineCwaConfig in a variant takes only name, styles and allowMultiple. name is the label in the UI select; without it, the select shows CwaComponentQuoteUiCard.

A variant's styles

A variant without styles uses the default template's, so the Card offers Boxed and Centred too. Those classes go on the Card's root element instead of the <figure>.

Give the variant its own styles when the default's don't suit it. They replace the default's: the Card then offers only its own.

defineCwaConfig({
  name: 'Card',
  styles: {
    shadow: { label: 'Shadow', classes: 'shadow-xl' },
  },
})

When an editor switches a Quote to another UI, it keeps only the stored styles the new one also defines. Switching a Quote styled Boxed to this Card drops Boxed, because the Card defines only shadow.

VIDEO: On the UI tab, switching the UI select from Default to Card: the pull quote is replaced by the dark card on the page, and the Style select now offers only Shadow
Keep other files out of ui/ and admin/. Every file in ui/ is a variant and every file in admin/ is a tab. A helper component the Quote uses goes in Quote/_parts/, which the module ignores. See Component Files.

When to use a variant and when to use a style

Use a UI variant when the look needs different HTML: different elements, different child components, different markup. The Card is a variant: it isn't a pull quote with other classes.

Use styles when it's the same HTML with different classes, or a small change you can make with styleKeys, such as showing an extra element.


Real Example: HtmlContent

The HtmlContent component, in the template and in the module's playground, uses both. HtmlContent/HtmlContent.vue declares one style, with allowMultiple: true so it can be combined with any others you add:

defineCwaConfig({
  name: 'Body Text',
  description: 'Paragraphs, headings, lists and links, styled to match the site',
  icon: 'rich-text',
  group: 'Text',
  allowMultiple: true,
  styles: {
    'black-bg': { label: 'Black Background', classes: 'bg-black border border-white p-2' },
  },
})

HtmlContent/ui/AltHtmlContent.vue is a different template, an <article> with a logo above the content, named with defineCwaConfig({ name: 'Alternative' }). It declares no styles, so it offers Black Background too. It uses styleKeys to add its own background only when no style is chosen.

Next: Your First Dynamic Page

So far each page has its own URL. Next, build a dynamic page in Your First Dynamic Page: news articles that share one template page, each at its own URL.