Action with AlpineJS
My issue is that when I click on the empty part of the widget its like I pressed the action. In other words, the action is supposed to do something, and whether I press the action or the empty parts of the widget, its the same. Any idea why? Im thinking it has something to do with the <div> but I dont know how else to structure it.
35 Replies
Because of this
was thinking of doing something like this, but it didnt work
Can anyone pls help?
Instead of the div with
{{ $this->testAction }}
, I think you could use a Filament button instead:
This does the job. Thank you. :)
One more thing. I might ive missed it in the docs, but how do I import code from a js file to blade? I dont want to have all of my code in header.
For example import it from
resources/js/player.js
This is how you can load a custom JS file inside of a Panel:
https://filamentphp.com/docs/3.x/support/assets#registering-javascript-files
Nice
Correct me if Im wrong, but should there be a specific structure to the JS file?
For example, this is the first line in my JS file:
But I get error in console:
Uncaught TypeError: Cannot read properties of undefined (reading 'item')
Yeah, that's not going to work. But you're pretty close!
Your JS file should register an Alpine component. Like the first example here with Alpine.data()
https://alpinejs.dev/globals/alpine-data
Yeah I figured đ
Ahhh I see
If your component is for example
Alpine.data('audioElement', ...)
, then you can use this directly in x-data="audioElement"
Got itt. I will give this a try. Thank you!!
amazingg. It worked!
Could I also use AlpineComponent instead?
I have this js code in the
public/js/app/local-test-script.js
directory
And the blade file:
Yet when I check the console, it cant find test
I also tried including the function in
Make sure the button is inside of the element with
x-data="audioElement"
It is (changed it a bit):
But it somehow STILL doesnt find it. And I have run
php artisan filament:assets
WTFFFF. So I had 2 JS files, and apparently even though i explicitly say which JS file to request, it requests a COMPLETELY different JS file.
UHH. So apparently if you have 2 JS files that have the same component 'audioElement', even if you call a SPECIFIC JS file, it will call the last one that is sees in the directory, even if you didnt call the file?? Pls correct me if Im wrong but that's what I understandI don't understand where the 2 files are coming from...
Yeah they were in the same directory, but they both had
audioElement
as component
But with different Alpine code.
Well, the blade file just took the last file with the audioElement
as componentOk... strange
I don't have much experience with
x-load-js
to be honest... I usually just load the JS file from a service provider.Might do that
Thank you for your help so far!
One more thing. I have my own theme, and I cant seem to be able to apply custom styles in the blade components. For example:
And of course
npm run build
. Do I have to register the css as well?It should work if you have configured a custom theme
https://filamentphp.com/docs/3.x/panels/themes#creating-a-custom-theme
Otherwise yeah, you can also load an extra CSS file from a service provider
I have created a custom theme and I also registed it in the admin panel
As far as I can tell, it only work for overriding classes that already exist in filament, such as
fi-section-content-ctn
I mean yeah I load the css, but how do I know where to place the
.test-widget
for example?
This just tells me that I need to load itI donât think the widget component inherits class attributes on its root. So just make a Div inside and add youâre classes there.
You should be able to add theme just fine to your alpine component div.
I already tried this, and at the time I thought I didnt work. but now, for debugging purposes, I added a red background to the class, and now I see that it indeed appears behind the
fi-section-content-ctn
As far as I can tell, it only work for overriding classes that already exist in filament, such as fi-section-content-ctnThis could be related to the
content
property of the tailwind config. Did you update it to include the views from your project?I think so yes:
Yeah, looks good
I will try and explain what Im looking for to the best of my abilities. What i need is a class selector that inherits all the properties of
fi-section-content-ctn
. I can make whatever modifications I want to it (the new class), and it wont affect fi-section-content-ctn
at allTherefore it works like a custom version of
fi-section-content-ctn
.Not sure Iâm really understanding the issue, but if it helps nothing in filament actually is based on any of the fi classes. They are merely a way to hook onto the components to override the default styling with your own stylesheet.
Ok so basically I want to have a custom version of
fi-section-content-ctn
but ONLY to that div that shown above. I dont want to touch the original fi-section-content-ctn
.So add a class higher up then your can scope it.
Thatâs standard css cascade.
Put a Div in your widget with a class of âwhateverâ then in your css .whatever .fi-section⌠it will be scoped.
I tried, but
Yea. So .test-widget .fi-etc
This is really css and not filament related. You need to figure out the cascade based on your app.
And I assume I have to apply this to the theme as well?
It should be in a theme either way. There is no too. Itâs either a theme or itâs not.
A theme replaces filaments default theme even if itâs based around the default theme. Thatâs just how tailwind works.
When you make a custom theme you arenât replacing just part of the default stylesheet that filament ships with. Itâs not an addition. Itâs a total replacement.