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

fix(templates): ensure consistency in using and recommending function/htm templates #5583

Merged
merged 5 commits into from Apr 7, 2023

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Apr 6, 2023

Summary

This fixes several places where we were either still having the "use html templates instead" warning by default, or wrongly documented.

Result

Each commit can be reviewed separately, they don't touch any overlapping files.

  • use Template in Pagination
  • improve warning when string template is used by giving the template key used
  • deprecate highlight/snippet helpers exposed from instantsearch (they return a html string)
  • use function templates in Panel (I checked all others, they were already migrated)
  • use function templates everywhere in storybook

⚠️ this PR should be rebased so each item shows up in the changelog separately

@Haroenv Haroenv requested review from a team, FabienMotte and aymeric-giraudet and removed request for a team April 6, 2023 14:55
@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 6, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 456f7b8:

Sandbox Source
InstantSearch.js Configuration
react-instantsearch-app Configuration
example-react-instantsearch-hooks-default-theme Configuration
example-vue-instantsearch-default-theme Configuration

@Haroenv Haroenv marked this pull request as draft April 6, 2023 15:10
This allows html in the pagination template without it being unsafe.
This adds the name of the template which is in string form (and thus deprecated)
These are superseded by the components in html tagged templates
@Haroenv Haroenv requested review from a team, FabienMotte and aymeric-giraudet and removed request for a team April 6, 2023 15:22
@Haroenv Haroenv marked this pull request as ready for review April 6, 2023 15:22
<div class="hits-image" style="background-image: url(${hit.image})"></div>
<article>
<header>
<strong>${components.Highlight({ hit, attribute: 'name' })}</strong>
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we rather do this :

const hitsItemTemplate = (hit, { components: { Highlight } }) => html`
  <strong><${Highlight} hit=${hit} attribute="name" /></strong>
`;

If we call the component as a function Preact won't be aware it's actually a component and won't show up in devtools.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it could be interesting, but in our documentation we only document the "function call" as it's much simpler to read and doesn't require people to know about preact.

Why doesn't it show up in the devtools though? it's still a preact component right?

Copy link
Member

Choose a reason for hiding this comment

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

Making a function call would be okay if these were just render functions but as they start with a capitalized letter and take a single prop argument, it's implied they are components which should be instantiated with Preact's h as they could use hooks.

They won't show up in Devtools in the VDOM as Highlight or Snippet components because they won't ever get instantiated by Preact's h function.

Now I know it's not a big deal yet as these components are in fact just render functions, but in the future if we provide any component using hooks this could be a problem :/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also do something like this developit/htm#167 (comment) (allowing the components to be a string), but I think both of those points are a separate discussion from this PR (as the PR just makes consistent what we already document), what do you think @aymeric-giraudet?

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes this could be even more user-friendly !
Indeed it's a different discussion, but it's good we're already aware of the eventual shortcomings we may face in the future :D
I'll review the rest !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#5584 it is possible, but I'm not convinced it's the way to go yet. We can do it easily if we want it.

@Haroenv Haroenv force-pushed the fix/templates-consistency branch 2 times, most recently from c89b3f1 to 6c789b6 Compare April 7, 2023 11:50
@Haroenv Haroenv merged commit 038601e into master Apr 7, 2023
4 checks passed
@Haroenv Haroenv deleted the fix/templates-consistency branch April 7, 2023 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants