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

Allow popovers examples to be tried on StackBlitz #36127

Conversation

julien-deramond
Copy link
Member

@julien-deramond julien-deramond commented Apr 7, 2022

This PR tries to allow popovers examples to be tried on StackBlitz.

In the documentation, they need some JS available in site/assets/js/snippets.js.
IMHO it would be troubling to have all this file integrated in StackBlitz.
So in this PR I'm trying to:

  • Split site/assets/js/snippets.js into small reusable parts
  • Create a new javascript parameter for the example shortcode containing the name of the reusable JS file to embed into StackBlitz
  • Allow site/layouts/partials/scripts.html to gather this information and embed the content of the JS file in a <script>.

Sub-tasks

  • Instead of using <script>, try to create a real index.js file in StackBlitz imported by index.html.
  • Find a better way to share the JS name between the doc, the shortcode and the site/layouts/partials/scripts.html
  • Security concerns to gather some JS file content?

Live previews

Not tackled here

  • Be able to pass multiple JS files (might be useful for other examples more complex)—we will see that while developing the fix for the other components
  • Be able to split the SCSS file used by examples and to pass it (them) like the JS files
  • (Optional) Hide some examples if too complex or impossible to try on StackBlitz

@julien-deramond julien-deramond changed the title [WIP] Allow popovers to be tested on StackBlitz [WIP] Allow popovers examples to be tried on StackBlitz Apr 7, 2022
@mdo
Copy link
Member

mdo commented Apr 14, 2022

I actually don't mind this solution... feels actually quite direct?

@julien-deramond
Copy link
Member Author

julien-deramond commented Apr 14, 2022

I actually don't mind this solution... feels actually quite direct?

Yep. I haven't found any other acceptable solutions atm so I'll continue with that atm.

In a separate commit (4e31bde) you'll find a version where the JS content is in a separate index.js file in StackBlitz.
For that, I simplified the configuration by saying that we always use the 'javascript' template (we could test if the content is empty or not and juggle with 'javascript' and 'html' templates if needed).
But to do that, I had to insert <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> in the <head> contrary to what's explained in the Starter Template.

@julien-deramond
Copy link
Member Author

julien-deramond commented Apr 16, 2022

Let's do it step by step. IMHO this PR can be reviewed as it is because it already offers an improvement for the users.
The description contains what's not done in this PR and could be tackled in other PRs in the same style while finishing step by step all the tasks tracked in #36091 (I could create a tracking issue instead of a PR if you prefer).

@julien-deramond julien-deramond marked this pull request as ready for review April 16, 2022 09:11
@julien-deramond julien-deramond requested a review from a team as a code owner April 16, 2022 09:11
@julien-deramond julien-deramond changed the title [WIP] Allow popovers examples to be tried on StackBlitz Allow popovers examples to be tried on StackBlitz Apr 16, 2022
@@ -48,15 +48,15 @@ var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {

We use JavaScript similar to the snippet above to render the following live popover. Titles are set via `title` attribute and body content is set via `data-bs-content`.

{{< example >}}
{{< example javascript="popover-demo.js" >}}
<button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" title="Popover title" data-bs-content="And here's some amazing content. It's very engaging. Right?">Click to toggle popover</button>
Copy link
Member Author

Choose a reason for hiding this comment

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

Don't know why but the code contains title="Popover title", it works in the doc but I can't find the title in the DOM and so it is missing in StackBlitz as well 🤔 If I replace title="Popover title" by data-bs-title="Popover title", everything's working correctly but I don't know the impact of such a change.

Copy link
Member

@GeoSot GeoSot May 3, 2022

Choose a reason for hiding this comment

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

Your answer is here unfortunately 😞

bootstrap/js/src/tooltip.js

Lines 513 to 525 in 51535cd

_fixTitle() {
const title = this._config.originalTitle
if (!title) {
return
}
if (!this._element.getAttribute('aria-label') && !this._element.textContent) {
this._element.setAttribute('aria-label', title)
}
this._element.removeAttribute('title')
}

Copy link
Member Author

Choose a reason for hiding this comment

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

OK so all popovers that contain titles won't be exportable in StackBlitz. StackBlitz button should be hidden for those cases. Except if someone thinks about a way of doing it differently.

Copy link
Member

Choose a reason for hiding this comment

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

This one is describing the title attribute fallback process, so it may is better to keep it as it was (no btn)

If we have other popovers using title, I think it's ok to change them using data-bs-title

@XhmikosR
Copy link
Member

We should really look into using esbuild for docs JS and thus split application.js into smaller files properly imported.

@julien-deramond
Copy link
Member Author

We should really look into using esbuild for docs JS and thus split application.js into smaller files properly imported.

Is it mandatory for this PR (and the nexts to come related to fix StackBlitz example) or can it be treated separately?

@mdo
Copy link
Member

mdo commented Apr 24, 2022

Definitely separately :)

@mdo mdo added this to In progress in v5.2.0-stable via automation Apr 30, 2022
@mdo mdo added this to In progress in v5.2.0 via automation Apr 30, 2022
@GeoSot GeoSot force-pushed the main-jd-stackblitz-and-popovers branch from 2aa3159 to abc01b0 Compare May 3, 2022 09:09
@GeoSot
Copy link
Member

GeoSot commented May 3, 2022

@julien-deramond Do you have any alternative in mind, to avoid this (using a link or something else)?

image

If we are in luck of alternatives, I suppose we can live with it

@julien-deramond
Copy link
Member Author

@julien-deramond Do you have any alternative in mind, to avoid this (using a link or something else)?

The only alternative I'm thinking of (not tested so maybe it is dumb) would be to do something like that:

  • Have site/assets/js/popover-demo.js accessible from https://getbootstrap.com/docs/5.1/assets/js/ (and not only within docs.min.js).
  • Still call the shortcode with example js_file="popover-demo.js" but then having data-js-file="popover-demo.js" (and not the content of the file)
  • When clicking on the button:
    • Request the JS file by concatenating {baseUrl}/docs/5.1/assets/js/ and popover-demo.js (now exported in assets/js)
    • Read its content
    • Export it as it is already done in this PR in StackBlitz.

If it works and is considered as a better solution please keep in mind that we would have several JS files used only for StackBlitz in assets/js; and then CSS files as well to fix the other issues with StackBlitz. It would also mean that for each Pull Request, we would have more files in Netlify and that can causes issues with some quotas you may have (the global package of the documentation would be heavier).

Do you want me to try this solution to see if it is feasible?

@GeoSot
Copy link
Member

GeoSot commented May 3, 2022

Do you want me to try this solution to see if it is feasible?

I am not the right person to give this answer, but it would come soon or late

\cc @mdo @XhmikosR

@mdo
Copy link
Member

mdo commented May 6, 2022

This will need a rebase or update to resolve a merge conflict :). We just merged a PR to split some more of the JS into separate files.

@julien-deramond julien-deramond force-pushed the main-jd-stackblitz-and-popovers branch from abc01b0 to 58986d3 Compare May 7, 2022 08:20
@GeoSot
Copy link
Member

GeoSot commented May 10, 2022

@julien-deramond in order to simplify a bit the whole process, can you investigate the case to make accessible & include by default the snippets.js?
If is is possible, we just need to add some clear comments about each functionality

@julien-deramond
Copy link
Member Author

julien-deramond commented May 10, 2022

Haven't tried it explicitly but it is possible to always include the content of this file in the index.js in StackBlitz rather than using the split files.

I rather thought to split it in order to have only what's needed for each example. If you think in the core team that's not a problem for the users who will share between them those examples with extra JS (and then extra CSS for the broken use cases of StackBlitz due to missing CSS) I can modify this PR in order to do that. It won't fix only popovers but probably a lot of examples at the same time.

As you said, in this case snippets.js would need some clear comments for the user to understand what's useful and when.

Edit: I missed the "make accessible" part of your comment in my first answer. I'll try to see how it can be accessed directly from outside. But if we stick with a single file (not a split one) maybe that's not useful. Let's try.

@julien-deramond
Copy link
Member Author

Closing this one superseded by #36352

v5.2.0-stable automation moved this from In progress to Done May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
v5.2.0-stable
  
Done
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants