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

Break the big component in compossable sub-components #81

Closed
stereobooster opened this issue Jul 15, 2018 · 8 comments
Closed

Break the big component in compossable sub-components #81

stereobooster opened this issue Jul 15, 2018 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@stereobooster
Copy link
Owner

stereobooster commented Jul 15, 2018

Try to break IdealImage into subcomponents, so developer can choose what features to use, for example:

Maybe try reakit approach or render-props#data.

@stereobooster stereobooster added the help wanted Extra attention is needed label Jul 15, 2018
@stereobooster
Copy link
Owner Author

stereobooster commented Jul 15, 2018

Lazy load

Lazy load can be an example of such subcomponent (aka primitive). It will detect if component on the screen or not and render placeholder, component itself or error state. It will take load function which will return Promise (or Promise-like thing), because it will work with Promise-like thing it could also handle dynamic import. It will know how to handle SSR.

Similar existing components:

Size detector

Component which will detect size of placeholder, so Image component can decide which image to load based on the size.

Similar existing components:

Other

Network detector, image loader (function, not a component), timeout canceler (function)

@stereobooster stereobooster changed the title Break the big component in compassable sub-components Break the big component in compossable sub-components Jul 16, 2018
@stereobooster
Copy link
Owner Author

I guess we can use React Hooks to try improve composability

@tuananh
Copy link

tuananh commented Dec 17, 2018

is there any update on how to disable network detection?

@hilja
Copy link

hilja commented Jan 8, 2019

This is interesting.

Here's some messy pseudocode on how things could work, it uses the Compound Components pattern that gives control to the markup more.

import React from 'react'
import IdealImage, {
  Lazyload,
  Wrapper,
  Image,
  GifVideo
} from 'react-ideal-image'

class Component extends React.Component {
  state = {
    hasZoomOnClick: true
  }

  handleSlow = () => {
    window.alert(
      'Looks like you‘re on a slow network, we have disabled images...'
    )
  }

  handleFail = () => {
    this.setState({
      hasZoomOnClick: false
    })
  }

  render() {
    return (
      <div>
        <Lazyload threshold={500}>
          <Wrapper
            // Override styles right in the component
            style={{ maxWidth: this.props.data.imageWidth, height: 'auto' }}
          >
            <Image
              // I've noticed how I'd like to execute some code on various stages
              onOffline={this.handleOffline}
              onFail={this.handleFail}
              onSlowNetwork={this.handleSlow}
              dimensions={{ w: 200, h: 200 }}
              placeholder={{ lqip }}
              styles={{ foo: 'foo' }}
            />
          </Wrapper>
        </Lazyload>

        {/* Image-like videos which loops and autoplays */}
        <Lazyload threshold={500}>
          <Wrapper>
            <GifVideo src={this.props.data.video.src} posterImage={{ lqip }} />
          </Wrapper>
        </Lazyload>

        {/* If control in not needed, use the all-inclusive keys-in-hand-solution */}
        <IdealImage srcSet={...} />
      </div>
    )
  }
}

export default Component

@scarletks1214
Copy link

Can we customize the icons and messages?
I can't find any detailed document or tutorial.
Please help me customize them.
Thanks

@stereobooster
Copy link
Owner Author

@scarletks1214
Copy link

scarletks1214 commented Feb 25, 2019 via email

@stereobooster
Copy link
Owner Author

Icons implemented as SVG in React component, you can read source code. Config implemented as key-value object where key is name and value is React component (SVG icon).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants