10up / wp-component-library

A library of barebones front-end components built with WordPress and accessibility in mind.
https://baseline.10up.com
MIT License
269 stars 42 forks source link

Ajax Load Pattern Component #167

Open joesnellpdx opened 7 years ago

joesnellpdx commented 7 years ago

It would be nice to have an example of ajax loading content (i.e. load more posts) as it seems to be different on every project. I'm not sure if the component library or best practices is the right place, but I'd sure love to know the 'ideal' patterns in a basic sense.

Of course, it is situational, but what is the best pattern for the following, or is there a general preference (situational excluded)?:

timwright12 commented 6 years ago

@joesnellpdx what do you think something like this would look like in a component?

joesnellpdx commented 6 years ago

@timwright12 Could go down a few directions here, but lately - I'm thinking a self contained vue component that can be passed params. A lot has changed since I wrote that question.

Maybe something I can put my mind to during my PRI. What do you think?

timwright12 commented 6 years ago

@joesnellpdx how about something that's platform independent, like a web component?

joesnellpdx commented 6 years ago

@timwright12

That is definitely a possibility. Good idea.

dkoo commented 5 years ago

I've done a lot of these in various projects and in my experience, the smoothest implementations have involved:

All that being said, I took a stab at putting something together as an exercise in thinking about how this could be componentized. You can see it here (most of the functionality exists in this JS file): https://github.com/dkoo/component-load-more/blob/master/src/load-more.js

Building out this sketch brought up some important questions that I think warrant further discussion here.

Other limitations of my proof of concept worth mentioning:

Anyway, I want to keep this discussion alive because it's something we build fairly often and I agree that the team could benefit from having a generalized approach to follow. I'm not sure what I've started should be that approach, but I think it's a good starting place to hash out some of these questions.

timwright12 commented 5 years ago

I think getting a component is a great idea and I like this as a starting point, but explicitly defining the template output like <h2 itemprop="headline"> is concerning if an h2 isn't the right answer for the page structure.

For it to be a true component in the library (and publish to NPM) it seems like we'd almost need to abstract everything out of the the file and into options and at some point it starts to become something else, right? Like a UI pattern or something that should be broken out into much smaller atomic elements and the result of collecting these elements together can be this loading pattern. Once it's broken down like that you can pass a lot of those decisions like CORS and sanitization onto the application rather than the component.

I could see us breaking the site out into components and patterns to get something like this sharable, because it definitely valuable.

dkoo commented 5 years ago

@timwright12 How would you recommend moving the markup to options? Should we have an option that lets you pass a template string of sorts, with the default matching our blogroll component?

Or are you suggesting that we think of creating a new category of "patterns" in the component library, where they're not NPM packages that you can drop into your application, but more like recipes you can follow?

timwright12 commented 5 years ago

@dkoo I'm thinking of a new category of patterns that could be a collection of npm packages. For example, this pattern could be an example of using npm packages XY and Z to accomplish a "load more posts" functionality