Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Twigcn Bundle Laravel Package

ducrot/twigcn-bundle

TwigcnBundle brings shadcn/ui-inspired, accessible UI components to Symfony & Twig. Install the PHP bundle plus the @ducrot/twigcn-ui NPM package and compile Tailwind CSS to use ready-made, themeable components in your templates.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup for First Use

  1. Install the Bundle

    composer require ducrot/twigcn-bundle
    

    The bundle auto-registers via Symfony Flex.

  2. Install NPM Dependencies

    npm install @ducrot/twigcn-ui
    

    For AssetMapper projects, expose the package:

    php bin/console importmap:require @ducrot/twigcn-ui
    
  3. Configure Tailwind Create a tailwind.css file at your project root (outside assets/):

    @import "tailwindcss";
    @import "@ducrot/twigcn-ui/styles";
    @source "../vendor/ducrot/twigcn-bundle/templates";
    @source "./templates/**/*.html.twig";
    

    Build with:

    npx @tailwindcss/cli -i tailwind.css -o assets/styles/app.css --watch
    
  4. Enable Stimulus Controllers Create/update assets/controllers.json:

    {
        "controllers": {
            "@ducrot/twigcn-ui": {
                "accordion": { "enabled": true },
                "dialog": { "enabled": true }
            }
        }
    }
    
  5. First Component Usage In a Twig template, use a component like:

    <twig:Twigcn:Button variant="primary">Click Me</twig:Twigcn:Button>
    

Implementation Patterns

Workflow: Component-Driven Development

  1. Component Selection Choose components from the Available Components section. Example: Use <twig:Twigcn:Card> for feature highlights or <twig:Twigcn:Tabs> for multi-step forms.

  2. Slot-Based Customization Leverage slots for nested content:

    <twig:Twigcn:Card>
        <twig:Twigcn:CardHeader>
            <h3>Feature Title</h3>
        </twig:Twigcn:CardHeader>
        <twig:Twigcn:CardContent>
            <p>Detailed content here.</p>
        </twig:Twigcn:CardContent>
    </twig:Twigcn:Card>
    
  3. Form Integration Pair form components with Symfony forms:

    <twig:Twigcn:Field>
        <twig:Twigcn:Label for="email">Email</twig:Twigcn:Label>
        {{ form_row(form.email) }}
    </twig:Twigcn:Field>
    
  4. Dynamic Data Binding Use Twig logic to conditionally render components:

    {% if user.hasPermission('admin') %}
        <twig:Twigcn:Button variant="destructive">Delete</twig:Twigcn:Button>
    {% endif %}
    
  5. Stimulus Interactivity Combine with Stimulus for dynamic behavior:

    <twig:Twigcn:DrawerTrigger for="settings-drawer" data-action="click->theme#toggleDrawer">
        Settings
    </twig:Twigcn:DrawerTrigger>
    
  6. Theming Consistency Override CSS variables in tailwind.css to maintain brand consistency:

    :root {
        --primary: {{ app.config.primary_color }};
        --secondary: {{ app.config.secondary_color }};
    }
    

Gotchas and Tips

Pitfalls

  1. Tailwind Class Scanning

    • Issue: Forgetting to add @source directives for new template paths causes Tailwind to miss classes.
    • Fix: Rebuild Tailwind after adding new templates:
      npx @tailwindcss/cli -i tailwind.css -o assets/styles/app.css --watch
      
  2. Stimulus Controller Registration

    • Issue: Components with Stimulus behavior (e.g., Dialog, Drawer) fail silently if controllers aren’t registered.
    • Fix: Verify controllers.json or bootstrap.js includes all required controllers. For Vite setups, ensure vite-plugin-symfony is configured.
  3. AssetMapper vs. Webpack Encore

    • Issue: Mixing AssetMapper and Webpack Encore can lead to duplicate or missing assets.
    • Fix: Stick to one asset pipeline. For Vite, use pentatrion/vite-bundle as shown in the README.
  4. Dark Mode Toggle

    • Issue: Dark mode doesn’t persist across page reloads.
    • Fix: Store the preference in localStorage or a cookie:
      document.documentElement.classList.toggle('dark');
      localStorage.setItem('theme', document.documentElement.className.includes('dark') ? 'dark' : 'light');
      
  5. Component Naming Conflicts

    • Issue: Some Twig tags differ from PHP class names (e.g., <twig:Twigcn:Empty> maps to EmptyState).
    • Fix: Refer to the Naming Notes section for mappings.
  6. Tailwind JIT Mode

    • Issue: Large projects may experience slow builds with Just-In-Time (JIT) Tailwind.
    • Fix: Disable JIT in tailwind.config.js if needed:
      module.exports = {
          jit: false,
      };
      

Debugging Tips

  1. Inspect Stimulus Controllers Check if controllers are loaded in the browser console:

    console.log(Stimulus.controllers);
    

    Ensure @ducrot/twigcn-ui controllers appear.

  2. Tailwind Class Errors Use the Tailwind CLI with --verbose to debug missing classes:

    npx @tailwindcss/cli -i tailwind.css -o assets/styles/app.css --verbose
    
  3. Symfony UX TwigComponent Debugging Enable Twig debug mode in config/packages/dev/twig.yaml:

    twig:
        debug: true
        strict_variables: true
    
  4. Vite/HMR Issues For Vite projects, clear the cache and rebuild:

    npm run build
    php bin/console cache:clear
    

Extension Points

  1. Custom Components Extend the bundle by creating new Twig components in src/Twigcn/ and register them in the bundle’s Resources/config/twigcn.php.

  2. Override Default Templates Override bundle templates by placing them in templates/bundles/Twigcn/. Example:

    templates/
        bundles/
            Twigcn/
                Button/
                    button.html.twig
    
  3. Add New Stimulus Controllers Extend the existing Stimulus controllers in @ducrot/twigcn-ui by importing and registering additional controllers in assets/controllers.json or bootstrap.js.

  4. Tailwind Plugin Integration Add custom Tailwind plugins to tailwind.config.js:

    module.exports = {
        plugins: [
            require('@ducrot/twigcn-ui/tailwind'),
            require('your-custom-plugin'),
        ],
    };
    
  5. Symfony Form Integration Create custom form types that render Twigcn components by extending AbstractType and overriding buildForm()/configureOptions(). Example:

    use Ducrot\TwigcnBundle\Form\TwigcnType;
    
    class CustomFormType extends AbstractType {
        public function buildForm(FormBuilderInterface $builder, array $options) {
            $builder->add('email', TwigcnType::class, [
                'component' => 'Input',
                'variant' => 'outline',
            ]);
        }
    }
    
  6. Accessibility Audits Use tools like axe to validate component accessibility. Twigcn components are designed to be WAI-ARIA compliant, but customizations may introduce issues.


```markdown
---
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php