GithubHelp home page GithubHelp logo

Comments (12)

getinnocuous avatar getinnocuous commented on May 20, 2024 2

legends! great work

from ladle.

cm-dyoshikawa avatar cm-dyoshikawa commented on May 20, 2024 1

Thanks. I will research it.

from ladle.

tajo avatar tajo commented on May 20, 2024 1

landed as part of v3.2, thanks to @cm-dyoshikawa

from ladle.

tajo avatar tajo commented on May 20, 2024

This could be pretty straightforward to implement - Ladle should preserve existing non-ladle params.

Relevant code: https://github.com/tajo/ladle/blob/main/packages/ladle/lib/app/src/history.ts#L25

from ladle.

cm-dyoshikawa avatar cm-dyoshikawa commented on May 20, 2024

Could I work on it?

from ladle.

tajo avatar tajo commented on May 20, 2024

Of course!

from ladle.

cm-dyoshikawa avatar cm-dyoshikawa commented on May 20, 2024

@tajo

Probably what @getinnocuous wants is a mechanism that allows setting query parameters for each Story. If implemented, it could make the cataloging of components that depend on the value of the query parameter more effective.

There exists an Addon in Storybook that realizes it. So, how about adding an Addon that can set query parameters in Ladle as well?

It will provide the following API:

import { Story } from "@ladle/react";

export const Hello: Story = () => {
  const urlSearchParams = new URLSearchParams(document.location.search);
  return (
    <>
      <h1>Hello Query Parameters</h1>
      <p>{urlSearchParams.get("foo")}</p>
    </>
  );
};

Hello.meta = {
  // Set query params
  query: {
    foo: "bar",
  },
};

from ladle.

tajo avatar tajo commented on May 20, 2024

I think @getinnocuous just wants to be able to programmatically set query params (aka their stories are reading/setting query params). Ladle now strips them away on any action - it should preserve them unless they collide with Ladle's params.

The add-on you describe is a different use-case. Not sure if its that useful since you can just use useEffect() to initialize query params.

from ladle.

cm-dyoshikawa avatar cm-dyoshikawa commented on May 20, 2024

OK. I considering.

from ladle.

cm-dyoshikawa avatar cm-dyoshikawa commented on May 20, 2024

Changing the code in the following way seems to make the combination of useEffect and history.pushState work

export const modifyParams = (globalState: GlobalState) => {
  if (!globalState.controlInitialized) return;
  const params = {
+   ...queryString.parse(location.search),
    mode: globalState.mode,
    rtl: globalState.rtl,
    source: globalState.source,
    story: globalState.story,
    theme: globalState.theme,
    width: globalState.width,
    control: globalState.control,
  };

Sample code for using the library:

import type { Story } from "@ladle/react";
import { useEffect, useState } from "react";

export const CardTest: Story = () => {
  const [queryParams, setQueryParams] = useState<string>("");

  useEffect(() => {
    setQueryParams(location.search);
  }, []);

  return <p>Params: {queryParams}</p>;
  // Expected => "Params: ?story=query-parameters--card-test&foo=bar"
  // Actual => "Params: ?story=query-parameters--card-test"
};

CardTest.decorators = [
  (Component) => {
    useEffect(() => {
      const url = new URL(window.location.href);
      url.searchParams.set("foo", "bar");
      history.pushState({}, "", url);
    }, []);

    return (
      <>
        <Component />
      </>
    );
  },
];

However, in this case as well, it seems challenging to capture the URL query parameters after the component has changed. Most React components that use query parameters are likely expected to obtain their value at the initial rendering with useEffect(() => {}, []). But, there are cases where these changes may not be ready at the time of this acquisition.

I want your opinion.

from ladle.

tajo avatar tajo commented on May 20, 2024
useEffect(() => {
  setQueryParams(location.search);
}, [location.search]);

would fix that?

from ladle.

cm-dyoshikawa avatar cm-dyoshikawa commented on May 20, 2024

Good point, thank you. I'll create the PR.

from ladle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.