Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Live] Add data-error attribute + fix loading behaviour on error #1916

Open
wants to merge 13 commits into
base: 2.x
Choose a base branch
from

Conversation

YummYume
Copy link
Contributor

Q A
Bug fix? yes
New feature? yes
Issues #1891
License MIT

This PR adds a new LoadingPlugin to live components. It allows the use of a data-error that works almost like the data-loading attribute, but for errors. It also fixes the loading state not clearing when an error occurs, and a typo in the hook names in the types.

Little demo of the before/after of this PR :

Before :

before_pr-2024-06-10_22.31.29.mp4

After :

after_pr-2024-06-11_09.33.28.mp4

@carsonbot carsonbot added Bug Bug Fix Feature New Feature Status: Needs Review Needs to be reviewed labels Jun 12, 2024
@smnandre
Copy link
Member

Thank you very much @YummYume ! I'll review in depth during week-end

A first comment though: i'm not sure we should expose the same API / possibilities, as such errors "should not" happen and we don't want to maintain/document too many things if they are not used.

So maybe could we select only one or two methods ? What do you think ?

Regarding the attribute, data-error maybe is a bit too common, especially with the added CSS.
Maybe data-live-error ?

(The documentation seems great, i think i'd inverse the 2 sections and start with the "what" before the "how")

Comment on lines 16 to 17
'loading.state:started': (element: HTMLElement, request: BackendRequest) => MaybePromise;
'loading.state:finished': (element: HTMLElement) => MaybePromise;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me some time to understand this ... as you have some CS changes mixed in here :)

Could you -if you have time- open a PR just for this "bug fix" ?

I know it's only TS and not really a bug, but people will probably see these values in their IDE... and i'd really like them to see the good ones instead 😅

And also to ensure we merge the fix asap .... because this one will depend on the path we choose to take :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do! I just added the typing to the hooks to not have to remember the event names and having a better DX, but I was wondering why my event wasn't firing... Only to find out there was a typo in the typing 😅

@YummYume
Copy link
Contributor Author

@smnandre Thanks for the feedback! 😄 By that, you mean only allowing the show|hide directives ? I currently implemented the same directives as the data-loading attribute because they were quite easy to add, but didn't add support for any modifier (like delay, models, and such...) as it wouldn't make much sense for an error state.

And I thought the same about the data-error attribute, I just wasn't sure if I should try to align its name with the data-loading one (since they are very similar). I will change it. Good point for the documentation as well !

@YummYume YummYume changed the title Add data-error attribute + fix loading behaviour on error [Live] Add data-error attribute + fix loading behaviour on error Jun 15, 2024
Copy link
Collaborator

@WebMamba WebMamba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wohaaaa! Thanks @YummYume for your PR! It's so cool! I love the path taking here!

I noticed that the data-live-error="addClass(text-red-500)" does not work. I didn't dig to see why but here is my template :

<div {{ attributes }}>
    <p data-live-error="addClass(text-red-500)">Hello</p>
    <button
            data-action="live#action"
            data-live-action-param="error"
    >
        Throw
    </button>
    <span data-live-error="show">An error occurred ! Please try again.</span>
</div>

the class text-red-500 is not added but the span is well added. Maybe you could have a clue.

I think we should disable the pop up we the error by default in production, and actually even in dev ? Do you think this is still needed since we have the request in the profiler ?

@YummYume
Copy link
Contributor Author

Thanks @WebMamba !

I just tried it, and it does work for me, the class is added on the element with the attribute.

image

Is it maybe just that the class does nothing because you're not using Tailwind ?

And about the error modal, I'm not sure, I think it's still a great and more direct debugging tool than the profiler. But for more in depth debugging, it's of course not ideal.

kbond added a commit that referenced this pull request Jun 26, 2024
This PR was merged into the 2.x branch.

Discussion
----------

[Live] Fix typing for loading hooks + HookManager

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        |
| License       | MIT

See #1916 (comment) for context

Fixes the typo on the loading hooks and add the hook typing to the `HookManager` (was forgotten in my first pr)

Commits
-------

9428547 Fix typing for loading hooks + HookManager
symfony-splitter pushed a commit to symfony/ux-live-component that referenced this pull request Jun 26, 2024
This PR was merged into the 2.x branch.

Discussion
----------

[Live] Fix typing for loading hooks + HookManager

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        |
| License       | MIT

See symfony/ux#1916 (comment) for context

Fixes the typo on the loading hooks and add the hook typing to the `HookManager` (was forgotten in my first pr)

Commits
-------

94285477 Fix typing for loading hooks + HookManager
@smnandre
Copy link
Member

Wherer are we on this one ? are you blocked by something ? Can i help you in any way @YummYume ?

@YummYume
Copy link
Contributor Author

Hey @smnandre ! I'll work on resolving the conflicts ASAP but other than that, no, I think it was all good !

If there are any suggestions or changes wanted to the current behavior, I'm all ears.

@smnandre
Copy link
Member

A couple of questions / comments, but to me we're almost there 😄

I'm still a bit doubtfull we need all the modifier and/or should maybe refactor the code avoid doubling the code with the existing ones, but this (refactor the existing) could be done in a later step / PR.

Do we really need the showError method for instance ? I mean do you think all the JS code can be simplified, more "direct" ? (genuine question)

Also, i'm not a big fan of the added CSS and would love for use to remove it entirely in future versions, so is there any way to handle this feature without adding CSS ?

@YummYume
Copy link
Contributor Author

Rebase is done.

Some of the code could perhaps be unified, but there was already a big part done (all the parsing is already shared between the loading and error plugins). I also made sure not to include too complex behaviors, such as targeting a specific action (which makes no sense here) or using modifiers with the attributes.

So with all that, I don't see a reason why not include them ? At least it stays consistent with the loading plugin. I did my best to make the plugin readable and easy to understand for any further modification, especially since this PR is technically only the first part of our long discussion on how to properly handle errors.

I agree with you for the CSS. On top of that, it doesn't always work. There can still a short window where you can actually see the element, until this style is loaded. Which is why (and I added this part in the doc as well), I always add my own display: none; class on my data-loading or data-live-error elements. I think the best solution would simply to remove the live.css stylesheet and warn the user about the need to add their own hidden class to their elements.

@@ -60,7 +60,7 @@
}

.dropzone-preview-button::before {
content: '×';
content: "×";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this :)

We'll make the change in another small PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never made that change, I think that's just the linter ?

@@ -1 +1 @@
.dropzone-container{position:relative;display:flex;min-height:100px;border:2px dashed #bbb;align-items:center;padding:20px 10px}.dropzone-input{position:absolute;display:block;top:0;left:0;width:100%;height:100%;opacity:0;cursor:pointer;z-index:1}.dropzone-preview{display:flex;align-items:center;max-width:100%}.dropzone-preview-image{flex-basis:0;min-width:50px;max-width:50px;height:50px;margin-right:10px;background-size:contain;background-position:50% 50%;background-repeat:no-repeat}.dropzone-preview-filename{word-wrap:anywhere}.dropzone-preview-button{position:absolute;top:0;right:0;z-index:1;border:none;margin:0;padding:0;width:auto;overflow:visible;background:0 0;color:inherit;font:inherit;line-height:normal;-webkit-font-smoothing:inherit;-moz-osx-font-smoothing:inherit;-webkit-appearance:none}.dropzone-preview-button::before{content:'×';padding:3px 7px;cursor:pointer}.dropzone-placeholder{flex-grow:1;text-align:center;color:#999}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same)

@smnandre
Copy link
Member

Thank you for the quick answers, make sense!

Let's update the src/LiveComponent CHANGELOG (it's gonna be 2.20)

@kbond @WebMamba @Kocal if one of you can give a second check ?

Comment on lines +193 to +199
private showElement(element: HTMLElement | SVGElement) {
element.style.display = 'revert';
}

private hideElement(element: HTMLElement | SVGElement) {
element.style.display = 'none';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the dev used style="display: flex" on element, what will happens when calling showElement?

Can we add/remove a custom CSS class instead, like data-live-hidden? It would be safer than manipulating element.style.display IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that sadly doesn't work, but this is a current issue with the data-loading attribute as well (see #102).
A simple workaround is to do something like class="hidden" data-live-error="addClass(flex) removeClass(hidden)" instead. That will work.

I also don't think adding more custom CSS would be a good idea, as discussed above, and we would probably have to use !important any way to avoid the style not taking effect.

Comment on lines +207 to +210

if (element.classList.length === 0) {
element.removeAttribute('class');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not necessary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact i believe it is, or the next morph will consider this a change .. or let's say i think i remember this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this behavior changed with the switch to Idiomorph though

src/LiveComponent/doc/index.rst Show resolved Hide resolved
src/LiveComponent/doc/index.rst Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants