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

Extract TableOfContents component to UI-Kit #67

Closed
6 tasks done
lottamus opened this issue Oct 1, 2019 · 0 comments
Closed
6 tasks done

Extract TableOfContents component to UI-Kit #67

lottamus opened this issue Oct 1, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@lottamus
Copy link
Contributor

lottamus commented Oct 1, 2019

Chore summary

We're starting to use the TableOfContents component in more places than just Docs, so it makes sense for us to make it a bit more generic and move it to @stoplight/ui-kit. BUT we should keeps the parts of the TableOfContents components that deal with Project nodes in this repo (see below).

Tasks

  • Move the ITableOfContents interface to ui-kit and remove the items prop:
export interface ITableOfContents {
- // List of items that will be computed into the tree structure
-  items?: IProjectNode[];

  contents: IContentsNode[];

  // Padding that will be used for (default: 10)
  padding?: string;
  className?: string;

  // Title of project
  title?: string;

  // Controls for the drawer functionality on mobile
  isOpen?: boolean;
  onClose?: () => void;

  // Mobile breakpoint, default (true) is 786px, false disables Drawer
  enableDrawer?: boolean | number;
}
import { TableOfContents as UIKitTableOfContents } from '@stoplight/ui-kit';

export const TableOfContents: React.FunctionComponent<ITableOfContents> = ({
  items,
  srn,
  ...props,
}) => {
  const contents = useComputeToc(items, srn);
  return <TableOfContents contents={contents} {...props}  />
};
  • On IContentsNode, replace the srn prop with isActive and add an optional href prop:
export interface IContentsNode {
  name: string;
  depth: number;
-  srn?: string;
+ isActive?: boolean;
+ href?: string;
  type?: 'divider' | 'group';
}
export function useComputeToc(nodes: IProjectNode[], srn?: string) {
contents.push({
  name: node.name,
-  srn: node.srn,
+ isActive: srn === node.srn,
+ href: srn,
  depth: parts.length - 1,
});

Example: (note the addition of "href" and the "isActive" prop to the last Github)

<TableOfContentsComponent
  contents={[
    {
      name: "Personal",
      depth: 0,
      type: "divider",
    },
    {
      name: "My Projects",
      depth: 0,
      icon: "star",
      href: '/projects/username',
    },
    {
      name: "GitHub",
      depth: 0,
      icon: "github",
      href: '/projects/username/github',
    },
    {
      name: "Stoplight Next",
      depth: 0,
      icon: "gitlab",
      href: '/projects/username/stoplight-next',
    },
    {
      name: "SendGrid",
      depth: 0,
      type: "divider"
    },
    {
      name: "GitHub",
      depth: 0,
      icon: "github",
      href: '/projects/sendgrid/github',
    },
    {
      name: "Stoplight Next",
      depth: 0,
      icon: "gitlab",
      href: '/projects/sendgrid/stoplight-next',
    },
    {
      name: "XYZ Corp",
      depth: 0,
      type: "divider"
    },
    {
      name: "GitHub",
      depth: 0,
      icon: "github",
      isActive: true,
      href: '/projects/xyz-corp/github',
    }
  ]}
/>

Result:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants