Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Unlocking Efficiency: Exploring Story within Story in Storybook

    Unlocking Efficiency: Exploring Story within Story in Storybook

    June 21, 2024

    Story within Story:

    In Storybook, “Story within Story” means using parts of one story in another. Instead of remaking stuff, developers just import existing stories and use their parts in a new story.

    This helps make complex UIs by combining simpler parts from different stories, making coding faster and more efficient.

    Suppose you have two components: one for a button (Button) and one for an input box (Input).
    Each component has its own set of stories defined in separate story files.
    If You want to combine these stories into a single-story file to showcase how they can be used together.
    Below is an example demonstrating how to achieve this by using “Story within Story” concept:

    Button.js:

    //Path src/Button/Button.js
    import React from ‘react’;
    import ‘./Button.css’;

    function Button(props) {
    const { variant = ‘Primary’, children, …rest } = props;
    return (
    <button className={`button ${variant}`} {…rest}>
    {children}
    </button>
    );
    }

    export default Button;

    Button.stories.js:

    //Path src/Button/Button.stories.js
    import React from ‘react’;
    import Button from ‘./Button’;

    export default {
    title: ‘Form/Button’,
    component: Button,
    };

    export const Primary = () => <Button variant=’primary’>Primary</Button>
    export const Secondary = () => <Button variant=’secondary’>Secondary</Button>
    export const Success = () => <Button variant=’success’>Success</Button>
    export const Danger = () =><Button variant=’danger’>Danger</Button>

    Input.js:

    //Path src/Input/Input.js
    import React from ‘react’;
    import ‘./Input.css’;

    const Input =(props) =>{
    const {size= ‘medium’, children, …rest}=props;
    return (
    <>
    <input type=”text” className={`input ${size}`} {…rest}/>
    </>
    )
    }

    export default Input

    Input.stories.js:

    //Path src/Input/Input.stories.js
    import React from ‘react’;
    import Input from ‘./Input’;

    export default {
    title: ‘Form/Input’,
    component: Input,
    };

    export const Small = () => <Input size=’small’ placeholder=’Small’>Small</Input>;
    export const Medium = () => <Input size=’medium’ placeholder=’Medium’>Medium</Input>;
    export const Large = () => <Input size=’large’ placeholder=’Large’>Large</Input>;

    Subscription.stories.js:

    In this example, the “Story within Story” concept is showcased.

    //Path src/Subscription/Subscription.stories.js
    import React from ‘react’;
    import { Secondary } from ‘../Button/Button.stories’;
    import { Large } from ‘../Input/Input.stories’;

    export default {
    title: ‘Form/Subscription’
    }

    export const SecondarySubscription = () => {
    return (
    <>
    <Secondary />
    <Large />
    </>
    );
    };

    Within the Storybook configuration file (Subscription.stories.js), the SecondarySubscription story is defined.
    It incorporates components from other stories (Button/Button.stories.js and Input/Input.stories.js), such as the Secondary button and Large input, respectively, rendering them within the SecondarySubscription

    Output:

    Advantages of “Story within Story” approach:

    Reduces Code Redundancy: Avoids the repetition of importing and defining props for components like the Secondary button and Large input, streamlining code.
    Automatic Updates: Modifications in original stories automatically propagate to the composite story. Changes made in the Secondary button story, for example, seamlessly update in the SecondarySubscription story.
    Enhanced Code Maintenance: Eliminates the need to manage code across multiple files, leading to improved code maintenance practices.
    Improved Efficiency: By reducing redundancy and automating updates, the approach enhances overall development efficiency, allowing developers to focus on building and refining UI elements effectively.

    Decorators:

    Decorators are component that wrap individual stories, providing additional functionality or styling.

    Let’s Understand with an example:

    Now, within this src directory, let’s establish a folder named ‘Center.’ Inside this folder, let’s generate two files: ‘Center.js’ for the Center component, and ‘Center.css’ for its associated styling.

    //Path src/Center/Center.js
    import React from ‘react’
    import ‘./Center.css’;
    const Center =(props) =>{
    return (
    <div className=’center’>
    {props.children}
    </div>
    )
    }
    export default Center
    //Path src/Center/Center.css
    .center{
    display: flex;
    justify-content: center;
    }

    Enclosing the Button’s “danger” variant within the Center component ensures centralized alignment without altering the Button’s code, promoting reusability.
    Adjustments to the centering feature can be made effortlessly by modifying or removing the Center component.

    //Path src/Button/Button.stories.js
    import React from ‘react’;
    import Button from ‘./Button’;
    import Center from ‘../Center/Center’;

    export default {
    title: ‘Form/Button’,
    component: Button,
    };

    export const Primary = () => <Button variant=’primary’>Primary</Button>
    export const Secondary = () => <Button variant=’secondary’>Secondary</Button>
    export const Success = () => <Button variant=’success’>Success</Button>
    export const Danger = () =><Center><Button variant=’danger’>Danger</Button></Center>

    Output:

    Now, to use the Center component as a decorator for the Button component, we can do the following:

    Storybook simplifies specifying decorators by enabling universal application after defining them once.
    Within the default export, the “decorators” property holds an array of functions. Each function wraps a story with a desired component, ensuring consistent application across stories effortlessly.

    //Path src/Button/Button.stories.js
    import React from ‘react’;
    import Button from ‘./Button’;
    import Center from ‘../Center/Center’;

    export default {
    title: ‘Form/Button’,
    component: Button,
    decorators: [story=><Center>{story()}</Center>]
    };

    export const Primary = () => <Button variant=’primary’>Primary</Button>
    export const Secondary = () => <Button variant=’secondary’>Secondary</Button>
    export const Success = () => <Button variant=’success’>Success</Button>
    export const Danger = () => <Button variant=’danger’>Danger</Button>

    Output:

    Global Decorators:

    If an input component lacks centered alignment, similar to the button component, we can address this by applying decorators in the default export.
    However, this could involve adding the decorator to every story in our Storybook.
    To simplify this process, we can utilize global decorators.

    //Path storybook/preview.js
    import React from ‘react’;
    import { addDecorator } from ‘@storybook/react’;
    import Center from ‘../src/Center/Center’;

    addDecorator(story=><Center>{story()}</Center>)

    Remove the addDecorator line from the //Path src/Button/Button.stories.js file, as we’ve defined this globally. Consequently, all components are now automatically centered.

    Conclusion:

    “Story within Story” method, along with decorators, makes UI development faster by reusing components, cutting down on repetition, and automating updates. This simplifies coding, maintenance, and encourages reusability for bigger projects.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMy health information has been stolen. Now what?
    Next Article Optimizing UI Development: Storybook Essentials

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-40906 – MongoDB BSON Serialization BSON::XS Multiple Vulnerabilities

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Florence-2: How it works and how to use it

    Artificial Intelligence

    Bumper Scuff Repair Essex, Brentwood, Chelmsford | Scratch & Scuff Repairs

    Web Development

    Usability and Experience (UX) in Universal Design Series: Challenges and Opportunities – 4

    Development

    Compliance Testing in eLearning

    Development

    Highlights

    This Guy’s Got 1000 NHL Points Shirt

    November 15, 2024

    Post Content Source: Read More 

    I used Copilot AI Vision to browse the web for me, and it has big potential

    December 23, 2024

    CVE-2025-20671 – Thermal Out-of-Bounds Write Privilege Escalation Vulnerability

    May 4, 2025

    North Korean Front Companies Impersonate U.S. IT Firms to Fund Missile Programs

    November 21, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.