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

Addon-controls: Blob control/arg type #13887

Closed
shilman opened this issue Feb 12, 2021 · 1 comment
Closed

Addon-controls: Blob control/arg type #13887

shilman opened this issue Feb 12, 2021 · 1 comment

Comments

@shilman
Copy link
Member

shilman commented Feb 12, 2021

As a developer,
I want to use non-serializable objects in controls,
so that I can choose between different {images, react components, etc.}

Currently, all keys and values must be JSON serializable (telejson-serializable, actually), which is a serious restriction when components can take arbitrary blob values / complex functions as their inputs.

Workaround

There is a workaround, which is to define an enum and then manually map it in the story function. For an example, see the last example in fully custom args docs:

export default {
  component: YourComponent,
  title: 'A complex case with a function',
  // creates specific argTypes with options
  argTypes: {
    propertyA: {
      control: {
        type: 'select',
        options: ['One', 'Two', 'Three'],
      },
    },
  }
};

const MAP = {
  One: <Foo />,
  Two: <Bar />
  Three: </Baz>
}

const Template = ({ propertyA, ...rest }) => {
  const someFunctionResult = MAP[propertyA];
  return <YourComponent someProperty={someFunctionResult} {...rest} />;
};

Proposal

We can make this much more ergonomic by providing a new ArgType and control for "blob" types.

export default {
  component: YourComponent,
  title: 'A complex case with a function',
  // creates specific argTypes with options
  argTypes: {
    propertyA: {
      control: {
        type: 'blob',
        values: {
          One: <Foo />,
          Two: <Bar />
          Three: </Baz>
        },
      },
    },
  },
};

const Template = (args) => {
  const someFunctionResult = MAP[propertyA];
  return <YourComponent {...args} />;
};

The blob ArgType is special because is implemented as a select control, but the Arg serialization serializes and displays the key, which must be be a serializable string. There needs to be some code in the preview rendering that maps that key to the blob value before the arg is received by the story function.

Related

This feature also makes the security restrictions introduced in URL handling more bearable: #13803

It can also be a workaround for #11429

@shilman shilman added this to the 6.2 controls milestone Feb 12, 2021
@shilman shilman changed the title Addon-controls: Blob control type Addon-controls: Blob control/arg type Feb 12, 2021
@shilman
Copy link
Member Author

shilman commented Feb 12, 2021

Better yet we could change the behavior of the select control to this, and we wouldn't need to introduce a new control/arg. Closing in favor of #13888

@shilman shilman closed this as completed Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant