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

investigate the macro-component syntax #17

Open
mathieudutour opened this issue Jan 31, 2018 · 2 comments
Open

investigate the macro-component syntax #17

mathieudutour opened this issue Jan 31, 2018 · 2 comments
Labels

Comments

@mathieudutour
Copy link
Member

there is a new lib (https://github.com/jxnblk/macro-components) that does pretty much what we are doing it but that have a lot more constraints and is more fragile (jxnblk/macro-components#3).

But the API is pretty neat, looks better than seapig. We could do something similar really easily tho:

instead of

import seapig, { OPTIONAL, REQUIRED } from 'seapig'

const Main = props => {
  const {
    sidebarChildren, // array of children with the `sidebar` prop
    contentChildren  // array of children with the `content` prop
  } = seapig(props.children, { // schema object
    sidebar: OPTIONAL, // sidebar is optional
    content: REQUIRED  // content is required
  })

  // rendering order is always the same
  return (
    <div>
      {sidebarChildren.length && <aside>{sidebarChildren}</aside>}
      <section>{content}</section>
    </div>
  )
}

we could have

import seapig from 'seapig'

const Main = seapig(({
    sidebar,
    content
  }) => {
  return (
    <div>
      {sidebar.length && <aside>{sidebar}</aside>}
      <section>{content}</section>
    </div>
  )
})

we lose the schema but maybe it can be an optional second argument to the seapig function

@nem035
Copy link
Member

nem035 commented Jan 31, 2018

That looks interesting. I wanna mess around with it but def like it at first look

@Downchuck
Copy link

Downchuck commented Feb 11, 2019

Could drop the Children suffix and keep the schema:

import seapig, { OPTIONAL, REQUIRED } from 'seapig'

const MainChildren = seapig({
    sidebar: OPTIONAL,
    content: REQUIRED
}) 

const Main = ({children}, context, {sidebar, content} = MainChildren(children)) => 
    <div>
      {sidebar.length && <aside>{sidebar}</aside>}
      <section>{content}</section>
    </div>;

Without a schema:

const seapig = (a,b,c='rest') =>
 React.Children.toArray(a).reduce(
   (obj, a) => {
     const k = b.find(b => b in a.props)||c;
     if(!obj[k]) obj[k]=[];
     obj[k].push(a);
     return obj
   }, {});

const Main = ({children}, context,
   {sidebar, content} = seapig(children, ['sidebar','content'])) =>
    <div>
      {sidebar.length && <aside>{sidebar}</aside>}
      <section>{content}</section>
    </div>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants