Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

feat(utils): add wrapper for the createComponent function #503

Merged
merged 10 commits into from
Nov 21, 2018

Conversation

mnajdova
Copy link
Contributor

@mnajdova mnajdova commented Nov 20, 2018

Introducing createComponent functionality as part of the public API in Stardust

This PR aims to fix #498 by adding a wrapper around the internal createComponent function. The API for the function looks like this:

export interface RenderStardustResultConfig {
  classes: ComponentSlotClasses
  rtl: boolean
}

export interface CreateStardustComponentConfig<P> {
  displayName: string
  render: (props: P & { stardust: RenderStardustResultConfig }) => React.ReactNode
}

const createComponent = <P extends {} = {}, S extends {} = {}>({
  displayName = 'StardustComponent',
  render,
}: CreateStardustComponentConfig<P>) => React.SFC<P> 

Only the bits related to the styles are added in the config of the component.

Usage example

Definition of the custom component

The createComponent function should be used for defining custom components in the following way:

export interface StyledButtonProps {
  styles?: ComponentSlotStyle
  variables?: ComponentVariablesInput
  children?: React.ReactNodeArray | React.ReactNode
}

const StyledButton: React.SFC<StyledButtonProps> = createComponent<StyledButtonProps, {}>({
  displayName: 'MyStyledButton',
  render({stardust, ...props}) {
    const { classes } = stardust
    
    return (<button className={cx(props.className, classes.root)} {...props} />)
  },
})

Example usage of the custom component

Some example usages of the StyledButton component would be:

Using styles and accessing the theme from the Provider

<StyledButton styles={({ theme: {siteVariables} }) => ({ backgroundColor: siteVariables.brand })}>
  My styled button
</StyledButton>

Using styles and accessing the props as they would use with some Stardust component

<StyledButton isRed styles={({props}) => ({backgroundColor: props.isRed ? 'red' : 'blue'})}>
  My red styled
</StyledButton>

Theming support

<Provider theme={{
  componentStyles: {
    StyledButton: {
      root: ({ props, variables, theme: {siteVariables} }) => ({
        backgroundColor: props.isRed ? (variables as any).red : siteVariables.brand,
      }),
    },
  },
  componentVariables: {
    StyledButton: {
      red: '#fc798c',
    },
  },
}}>
  <StyledButton isRed>My styled button on Provider</StyledButton>
</Provider>

@kuzhelov
Copy link
Contributor

kuzhelov commented Nov 20, 2018

@mnajdova, please, note that example's code provided in description could be simplified:

const StyledButton = createComponent({
  displayName: 'MyStyledButton',
  render(stardust, props) {
    const { classes } = stardust
    
    // note that we should respect client's classes passed as well
    return (<button className={cx(props.className, classes.root)} {...props} />)
  },
})

@codecov
Copy link

codecov bot commented Nov 20, 2018

Codecov Report

Merging #503 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #503      +/-   ##
==========================================
+ Coverage   88.41%   88.42%   +0.01%     
==========================================
  Files          42       42              
  Lines        1450     1452       +2     
  Branches      186      186              
==========================================
+ Hits         1282     1284       +2     
  Misses        163      163              
  Partials        5        5
Impacted Files Coverage Δ
src/index.ts 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4fb1fc2...0739bfb. Read the comment docs.

Copy link
Collaborator

@bmdalex bmdalex left a comment

Choose a reason for hiding this comment

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

left comments

src/lib/createStardustComponent.tsx Show resolved Hide resolved
src/lib/createStardustComponent.tsx Outdated Show resolved Hide resolved
src/lib/createStardustComponent.tsx Outdated Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
Copy link
Member

@levithomason levithomason left a comment

Choose a reason for hiding this comment

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

Minor changes requested, no blockers from me

src/lib/createStardustComponent.tsx Outdated Show resolved Hide resolved
@levithomason
Copy link
Member

Next up we'll publish some docs and examples showing how to use this. There should be a new Guide for "Custom Components".

@mnajdova mnajdova merged commit 7ada6ac into master Nov 21, 2018
@brianespinosa
Copy link
Collaborator

Sweet!

@layershifter layershifter deleted the feat/stardust-component branch January 10, 2019 11:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose a wrapper for createComponent
7 participants