Comments (12)
legends! great work
from ladle.
Thanks. I will research it.
from ladle.
landed as part of v3.2
, thanks to @cm-dyoshikawa
from ladle.
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.
Could I work on it?
from ladle.
Of course!
from ladle.
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.
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.
OK. I considering.
from ladle.
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.
useEffect(() => {
setQueryParams(location.search);
}, [location.search]);
would fix that?
from ladle.
Good point, thank you. I'll create the PR.
from ladle.
Related Issues (20)
- Side navigation icons zIndex should not be above the stories
- HMR does not seem to work on .ladle/components.tsx HOT 1
- After the upgrade 2.17.2 -> 3.0.0 our Jest snapshots have a new prop HOT 6
- Chevron icons in sidebar seem switched
- Way to verify that all stories render without errors HOT 6
- Side panel story name not readable when using longer names or folder nesting HOT 1
- Version 3 supports only React 18+, it's an undocumented breaking change HOT 1
- Deno support HOT 1
- Allow duplicate story name HOT 2
- Missing Accessibility Report button with @ladle/react v3.1.0 and up HOT 2
- ladle.css leaks on @reach/dialog inside stories HOT 1
- Internal server error: Header name must be a valid HTTP token [":path"] HOT 2
- ladle build error HOT 3
- Should the msw implementation be created as an independent plugin? HOT 2
- RTL config using MDX code block breaks formatting HOT 1
- Stylesheet ordering bug with @mui/material and styled-components HOT 6
- Head elements not being synchronized between the iframe and its parent
- Range control with a default value of zero does not adopt default value
- Source maps are broken
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ladle.