@01e9/react-page-layout 中文文档教程

发布于 5年前 浏览 30 项目主页 更新于 3年前

稳定版license

About

这个库允许你共享一个通用布局跨越许多不同的组件。 与 react-router 之类的东西一起使用非常有用,可以跨多个路径抽象设计的主要元素并减少代码重复。

Installation

npm install @01e9/react-page-layout --save

或者如果你使用 yarn

yarn add @01e9/react-page-layout

Preview

Preview

Example

这个 repo 下有一个例子目录。 在克隆回购中运行。 转到示例目录 并运行 npm install; npm start

Overview

该库允许您将所有布局逻辑集中到它自己的组件中。 它允许您创建一个布局,该布局可以有多个 ,您可以在其中注入内容。

布局感知组件可以使用

来填充插槽。

Setup

  1. 第一步是您必须创建一个布局组件,这是一个常规的 React 组件,具有多个可以注入内容的 插槽。 您可以使用 组件并为它们命名来定义一个或多个插槽。 在这种情况下,“main”

    import React, { Component } from 'react';
    import { Slot } from '@01e9/react-page-layout';
    
    class PublicLayout extends Component {
        render() {
            return (
                <div>
                    <header>Page Header</header>
                    <Slot name="main" />
                    <footer>Page Footer</footer>
                </div>
            );
        }
    }
    
    export default PublicLayout;
    
  2. 您必须让您的应用程序布局感知。 为此,您使用 组件让后代了解您应用的不同布局。 它需要你用一个对象指定它的 layouts 属性,其中键是名称,值是布局组件;

    import React, { Component } from 'react';
    import { render } from 'react-dom';
    import { LayoutProvider } from '@01e9/react-page-layout';
    import PublicLayout from './layouts/public';
    import LoginPage from './pages/login';
    
    // Create a map for all your layouts 
    const layouts = {
        'public': PublicLayout,
    };
    
    class App extends Component {
    function render() { 
    
        // Render your page inside
        // the layout provider
        return (
            &lt;LayoutProvider layouts={layouts}&gt;
                &lt;LoginPage /&gt;
            &lt;/LayoutProvider&gt;
        );
    }
    
    } render(App, document.getElementById('root'));

    注意:您的页面组件不必是 的直接后代,事实上您可能永远不会以这种方式使用它。 这仅用于说明目的。 此功能允许您将此包与任何路由库一起使用。

  3. 接下来您必须创建页面。 这些页面为布局中的不同插槽提供内容。 为此,我们使用了两个组件,

    import { Page, Section } from '@01e9/react-page-layout';
    
    class LoginPage extends Component {
            render() {
                return (
                    <Page layout="public">
                        <Section slot="main">
                            <h1> THIS IS THE PAGE CONTENT </h1>
                        </Section>
                    </div>
                );
            }
    }
    

    您必须将 layout 属性传递给 ,以便它知道您要使用的布局。 同样,每个

    都有一个 slot 属性 将它与它提供内容的布局中的插槽联系起来。 在这种情况下 因为我们只有一个名为 public 的布局,而且它只有一个 插槽将其命名为 ma​​in 我们使用这些值。

Usage

  1. 默认情况下,插槽的根是一个 div,但可以通过其 属性对其进行自定义。 您还可以自定义类名和样式。 有关详细信息,请参阅文档。

  2. 您可以在布局中使用多个插槽。

        ...
        <Slot name="header" component="header" />
        <Slot name="content" component="main" />
        <Slot name="footer" component="footer" />
        ...
    
  3. 插槽可以有默认内容,当没有指定相应部分时将使用默认内容

        ...
        <Slot name="header" component="header">
            Default Header content
        </Slot>
        ...
    
  4. 如果插槽没有内容,它根本不会呈现。 这意味着 dom 不包含该插槽的任何元素。 这很有用,因为您不必在周围放置额外的元素。

  5. 传递给 组件的任何道具都会传递给布局。 这对于标题、面包屑或标记页面所需的任何自定义很有用。

  6. 插槽可以嵌套

        ...
        <Slot name="main" component="main">
            Ill show up before content
        &lt;Slot 
            name="content" 
            component="div" 
            style={{ margin: 20 }}
        /&gt;
    
        Ill show up after content
    &lt;/Slot&gt;
    ...
    

    在这种情况下,名称为 content有一个边距。 ma​​in 插槽有额外的内容。 对应的页面可以决定,它要展示什么 通过使用相应的插槽。

        ...
        <Page layout="mylayout">
            <Section slot="content"> 
                I have things on top and after me, and
                I have a margin
            </Section>
        </Slot>
        ...
    
        ...
        <Page layout="mylayout">
            <Section slot="main"> 
                Im alone and margin-less
            </Section>
        </Slot>
        ...
    

Api

<LayoutProvider>

布局提供程序,使您的应用程序布局感知。 应该放在 尽可能高地进入组件层次结构。

PropDescriptionrequired
layoutsMap of all the component layouts, with keys being the names, and the values the componentsYes

<Slot>

PropDescriptionDefaultrequired
nameThe name of the slot-Yes
componentIt allows you to determine the root component for this slot, for example you could pass "header" create a <header> elementdivNo
wrapperIf you don't want to specify a component you can specify an element of your choice, for example <MyComponent className="red" />-No
classNameclass to be added to the root of this slot-No
stylestyles to be appended to the root of this slot-{}

<Page>

PropDescriptionDefaultrequired
layoutThe name of the layout used to render this page-Yes
Any other prop, will be passed as is, to the layout-No

<Section>

PropDescriptionDefaultrequired
slotThe name of the slot this section is providing content for-Yes

Stable Releaselicense

About

This library allows you to share a common layout across many different components. Very useful with something like react-router to abstract the main elements of your design across many routes and reducing code duplication.

Installation

npm install @01e9/react-page-layout --save

or if you use yarn

yarn add @01e9/react-page-layout

Preview

Preview

Example

There is an example in this repo under the examples directory. To run in clone the repo. Go to the examples directory and run npm install; npm start

Overview

This library allows for you to concentrate all your layout logic into its own components. It allows you to create a layout that can have serveral <Slots> where you can inject content.

A layout aware component can use the <Page> and <Section> to fill out the slots.

Setup

  1. First Step is that you have to create a layout component, this is a regular react component that has several slots where content can be injected. You define one or more slots by using the <Slot> component and giving them a name. In this case "main"

    import React, { Component } from 'react';
    import { Slot } from '@01e9/react-page-layout';
    
    class PublicLayout extends Component {
        render() {
            return (
                <div>
                    <header>Page Header</header>
                    <Slot name="main" />
                    <footer>Page Footer</footer>
                </div>
            );
        }
    }
    
    export default PublicLayout;
    
  2. You have to make your app layout aware. In order to do this you use the <LayoutProvider> component to let decendants know about the different layouts of your app. It requires that you specify its layouts prop with an object, where the keys are the names and the values are the layout components;

    import React, { Component } from 'react';
    import { render } from 'react-dom';
    import { LayoutProvider } from '@01e9/react-page-layout';
    import PublicLayout from './layouts/public';
    import LoginPage from './pages/login';
    
    // Create a map for all your layouts 
    const layouts = {
        'public': PublicLayout,
    };
    
    class App extends Component {
    function render() { 
    
        // Render your page inside
        // the layout provider
        return (
            &lt;LayoutProvider layouts={layouts}&gt;
                &lt;LoginPage /&gt;
            &lt;/LayoutProvider&gt;
        );
    }
    
    } render(App, document.getElementById('root'));

    Note: Your page components don't have to be direct decendents of the <LayoutProvider>, in fact you probably will never use it this way. This is for illustration purposes only. This feature is what allows you to use this package with any routing library.

  3. Next you have to create pages. The pages provide the content for the different slots in your layout. To do this we use two components, <Page> and <Section>.

    import { Page, Section } from '@01e9/react-page-layout';
    
    class LoginPage extends Component {
            render() {
                return (
                    <Page layout="public">
                        <Section slot="main">
                            <h1> THIS IS THE PAGE CONTENT </h1>
                        </Section>
                    </div>
                );
            }
    }
    

    You have to pass the layout property to the <Page>, so it knows what layout you want to use. Similarly each <Section> has a slot property that ties it to the slot in the layout for which it provides content. In this case since we only have one layout named public, and it only has one slot named it main we use those values.

Usage

  1. By default, the root of a slot is a div, but it can be customized via its the <Slot> props. You can also customize className and styles. See the documentation for details.

  2. You can use several slots in a layout.

        ...
        <Slot name="header" component="header" />
        <Slot name="content" component="main" />
        <Slot name="footer" component="footer" />
        ...
    
  3. A slot can have default content, that will be used when no corresponding section is specified

        ...
        <Slot name="header" component="header">
            Default Header content
        </Slot>
        ...
    
  4. If a slot doesn't have content it doesn't render at all. Meaning that the dom doesn't contain any elements for that slot. This is usefull because you don't have to have extra elements lying arround.

  5. Any props passed to the <Page> component are passed to the layout. This can be usefull for titles, breadcrumbs or to flag any customization that the page requires.

  6. Slots can be nested

        ...
        <Slot name="main" component="main">
            Ill show up before content
        &lt;Slot 
            name="content" 
            component="div" 
            style={{ margin: 20 }}
        /&gt;
    
        Ill show up after content
    &lt;/Slot&gt;
    ...
    

    In this case the <Slot> with name content has a margin. And the main slot has extra content. The corresponding page can decide, what it wants to show by using the corresponding slot.

        ...
        <Page layout="mylayout">
            <Section slot="content"> 
                I have things on top and after me, and
                I have a margin
            </Section>
        </Slot>
        ...
    
        ...
        <Page layout="mylayout">
            <Section slot="main"> 
                Im alone and margin-less
            </Section>
        </Slot>
        ...
    

Api

<LayoutProvider>

The layout provider, makes your app layout aware. It should be placed as high as possible into the component hierarchy.

PropDescriptionrequired
layoutsMap of all the component layouts, with keys being the names, and the values the componentsYes

<Slot>

PropDescriptionDefaultrequired
nameThe name of the slot-Yes
componentIt allows you to determine the root component for this slot, for example you could pass "header" create a <header> elementdivNo
wrapperIf you don't want to specify a component you can specify an element of your choice, for example <MyComponent className="red" />-No
classNameclass to be added to the root of this slot-No
stylestyles to be appended to the root of this slot-{}

<Page>

PropDescriptionDefaultrequired
layoutThe name of the layout used to render this page-Yes
Any other prop, will be passed as is, to the layout-No

<Section>

PropDescriptionDefaultrequired
slotThe name of the slot this section is providing content for-Yes
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文