Skip to content

Symfony UX

Vous pouvez utiliser les composants de Symfony UX dans votre application. Les composants symfony/ux-react, symfony/ux-vue et symfony/ux-svelte nécessitent toutefois quelques ajustements. Voir leur sections dédiées.

UX packagesCompatibilityUX packagesCompatibility
ux-autocompleteux-svelte✅ (*)
ux-chartjsux-swup
ux-cropperjsux-toggle
ux-dropzoneux-translator
ux-lazyux-turboPas encore testé (**)
ux-live-componentux-twig
ux-notifyux-typed
ux-react✅ (*)ux-vue✅ (*)

(*) demande quelques modifications au niveau des imports (voir ci-dessous) (**) en théorie il n'y a aucune raison que ce ne soit pas compatible, vos retours sont les bienvenus.

Prérequis

bash
composer require symfony/stimulus-bundle

# désinstallez le package @symfony/stimulus-bridge
# uniquement compatible webpack
npm rm @symfony/stimulus-bridge

symfony/ux-vue

Installation

bash
composer require symfony/ux-vue
npm i --force

Après l'installation de la recette Flex de symfony/ux-vue vous aurez besoin de corriger ces lignes.

js
import { registerVueControllerComponents } from '@symfony/ux-vue'; 
registerVueControllerComponents(require.context('./vue/controllers', true, /\.vue$/)); 

import { startStimulusApp, registerControllers } from "vite-plugin-symfony/stimulus/helpers"
import { registerVueControllerComponents } from "vite-plugin-symfony/stimulus/helpers/vue"

// register Vue components before startStimulusApp
registerVueControllerComponents(import.meta.glob('./vue/controllers/**/*.vue')) 

const app = startStimulusApp();
registerControllers(
  app,
  import.meta.glob(
    "./controllers/*_controller.js",
    {
      query: "?stimulus",
      eager: true,
    },
  ),
);
ts
import { registerVueControllerComponents } from '@symfony/ux-vue'; 
registerVueControllerComponents(require.context('./vue/controllers', true, /\.vue$/)); 

import { startStimulusApp, registerControllers } from "vite-plugin-symfony/stimulus/helpers"
import { registerVueControllerComponents, type VueModule } from "vite-plugin-symfony/stimulus/helpers/vue"

// register Vue components before startStimulusApp
registerVueControllerComponents(import.meta.glob<VueModule>("./vue/controllers/**/*.vue")); 

const app = startStimulusApp();
registerControllers(
  app,
  import.meta.glob<StimulusControllerInfosImport>(
    "./controllers/*_controller.ts",
    {
      query: "?stimulus",
      eager: true,
    },
  ),
);
js
// vite.config.js
import { defineConfig } from 'vite'

import symfonyPlugin from 'vite-plugin-symfony';
import vuePlugin from "@vitejs/plugin-vue";

export default defineConfig({
  plugins: [
    vuePlugin(), 
    symfonyPlugin({
      stimulus: true
    }),
  ],
  build: {
    rollupOptions: {
      input: {
        "app": "./assets/app.js",
      }
    },
  },
});
twig
{# your-template.html.twig #}
<div {{ vue_component('Hello', { 'name': 'Vite & Stimulus' }) }}></div>
vue
<!-- assets/vue/controllers/Hello.vue -->
<template>
    <div>Hello {{ name }}!</div>
</template>

<script setup>
    defineProps({
        name: String
    });
</script>

Documentation : Symfony doc, Symfony UX.

symfony/ux-react

Installation

bash
composer require symfony/ux-react
npm i --force

Après avoir installé la recette Flex depuis symfony/ux-react, vous devrez corriger ces lignes.

js
import { registerReactControllerComponents } from '@symfony/ux-react'; 
registerReactControllerComponents(require.context('./react/controllers', true, /\.(j|t)sx?$/)); 

import { startStimulusApp, registerControllers } from "vite-plugin-symfony/stimulus/helpers"
import { registerReactControllerComponents } from "vite-plugin-symfony/stimulus/helpers/react"

registerReactControllerComponents(import.meta.glob('./react/controllers/**/*.[jt]s(x)\?')); 

const app = startStimulusApp();
registerControllers(
  app,
  import.meta.glob(
    "./controllers/*_controller.js",
    {
      query: "?stimulus",
      eager: true,
    },
  ),
);
ts
import { registerReactControllerComponents } from '@symfony/ux-react'; 
registerReactControllerComponents(require.context('./react/controllers', true, /\.(j|t)sx?$/)); 

import { startStimulusApp, registerControllers } from "vite-plugin-symfony/stimulus/helpers"
import { registerReactControllerComponents, type ReactModule } from "vite-plugin-symfony/stimulus/helpers/react"

// enregistrez vos composants React avant startStimulusApp
registerReactControllerComponents( 
  import.meta.glob<ReactModule>("./react/controllers/**/*.[jt]s(x)?"), 
); 

const app = startStimulusApp();
registerControllers(
  app,
  import.meta.glob<StimulusControllerInfosImport>(
    "./controllers/*_controller.ts",
    {
      query: "?stimulus",
      eager: true,
    },
  ),
);

Parce que registerReactControllerComponents a été invoqué avec import.meta.glob en mode lazy, vous devez définir dans votre controllers.json fetch eager (sinon vous aurez des imbrications de promesses).

json
{
    "controllers": {
        "@symfony/ux-react": {
            "react": {
                "enabled": true,
                "fetch": "eager"
            }
        },

    },
    "entrypoints": []
}
js
// vite.config.js
import { defineConfig } from 'vite'

import symfonyPlugin from 'vite-plugin-symfony';
import reactPlugin from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [
    reactPlugin(), 
    symfonyPlugin({
      stimulus: true
    }),
  ],
  build: {
    rollupOptions: {
      input: {
        "app": "./assets/app.js",
      }
    },
  },
});
twig
{# base.html.twig #}
{{ vite_entry_link_tags('app') }}
{{ vite_entry_script_tags('app', {
    dependency: 'react'
  }) }}
twig
{# your-template.html.twig #}
<div {{ react_component('Hello', { 'fullName': 'Vite & Stimulus' }) }}></div>
jsx
// assets/react/controllers/Hello.jsx
import React from 'react';

export default function (props) {
    return <div>Hello {props.fullName}</div>;
}

Documentation : Symfony doc, Symfony UX React.

symfony/ux-svelte

Installation

bash
composer require symfony/ux-svelte
npm i --force

Après avoir installé la recette Flex depuis symfony/ux-svelte, vous devrez corriger ces lignes.

js
import { registerSvelteControllerComponents } from '@symfony/ux-svelte'; 
registerSvelteControllerComponents(require.context('./svelte/controllers', true, /\.svelte$/)); 

import { startStimulusApp, registerControllers } from "vite-plugin-symfony/stimulus/helpers"
import { registerSvelteControllerComponents } from "vite-plugin-symfony/stimulus/helpers/svelte"
registerSvelteControllerComponents(import.meta.glob('./svelte/controllers/**/*.svelte')); 

const app = startStimulusApp();
registerControllers(
  app,
  import.meta.glob(
    "./controllers/*_controller.js",
    {
      query: "?stimulus",
      eager: true,
    },
  ),
);
ts
import { registerSvelteControllerComponents } from '@symfony/ux-svelte'; 
registerSvelteControllerComponents(require.context('./svelte/controllers', true, /\.svelte$/)); 

import { startStimulusApp, registerControllers } from "vite-plugin-symfony/stimulus/helpers"
import { registerSvelteControllerComponents, type SvelteModule } from "vite-plugin-symfony/stimulus/helpers/svelte"

registerSvelteControllerComponents( 
  import.meta.glob<SvelteModule>("./svelte/controllers/**/*.svelte"), 
); 

const app = startStimulusApp();
registerControllers(
  app,
  import.meta.glob<StimulusControllerInfosImport>(
    "./controllers/*_controller.ts",
    {
      query: "?stimulus",
      eager: true,
    },
  ),
);

Parce que registerSvelteControllerComponents a été invoqué avec import.meta.glob en mode lazy, vous devez définir dans votre controllers.json fetch eager (sinon vous aurez des imbrications de promesses).

json
{
    "controllers": {
        "@symfony/ux-svelte": {
            "svelte": {
                "enabled": true,
                "fetch": "eager"
            }
        },

    },
    "entrypoints": []
}
js
// vite.config.js
import { defineConfig } from 'vite'

import symfonyPlugin from 'vite-plugin-symfony';
import { svelte } from '@sveltejs/vite-plugin-svelte'

export default defineConfig({
  plugins: [
    svelte(), 
    symfonyPlugin({
      stimulus: true
    }),
  ],
  build: {
    rollupOptions: {
      input: {
        "app": "./assets/app.js",
      }
    },
  },
});
js
// svelte.config.js
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'

export default {
  // Consult https://svelte.dev/docs#compile-time-svelte-preprocess
  // for more information about preprocessors
  preprocess: vitePreprocess(),
}
twig
{# your-template.html.twig #}
<div {{ svelte_component('Hello', { 'name': 'Vite & Stimulus' }) }}></div>
svelte
<!-- assets/svelte/controllers/Hello.svelte -->
<script>
    export let name = "Svelte";
</script>

<div>Hello {name}</div>

Documentation : Symfony doc, Symfony UX Svelte.

Publié sous la licence MIT.