Skip to content

feat(v2): Add @theme-init components to user theme#3485

Merged
slorber merged 2 commits intofacebook:masterfrom
edno:refactor/init-theme-user
Sep 28, 2020
Merged

feat(v2): Add @theme-init components to user theme#3485
slorber merged 2 commits intofacebook:masterfrom
edno:refactor/init-theme-user

Conversation

@edno
Copy link
Contributor

@edno edno commented Sep 25, 2020

Motivation

Swizzled components currently do not have access to @theme-init components as it has been introduced in PR#2464 for Docusaurus components.

Having access to initial theme components allows overriding default components without having to rewrite the entire component.

This PR extend the initial PR for providing access to @theme-init components for custom components.

The point has been raised on the Discord channel #docusaurus-2-dogfooding.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Tested with a local instance and a custom code block:

import React from 'react';
import CodeBlock from '@theme-init/CodeBlock'; // access to initial theme Codeblock

const TryQueryLink = ({ gql, text }) => {
  const graphiQLPath = "/graphiql?query=";
  const encodedQuery = encodeURIComponent(gql || "");
  return <a href={`${graphiQLPath}${encodedQuery}`}>{text}</a>;
}

const graphiQLCodeBlock = (Component) => {
  const WrappedComponent = (props) => {
    let tryMe;
    const language = props.className && props.className.replace(/language-/, '') || "";
    if (props.withgraphiql && language == "graphql") {
      const match = props.metastring && props.metastring.match(/withgraphiql/, RegExp.$1);
      tryMe = <TryQueryLink
 gql={props.children} text="Try Query"/>;
    }

    return <>{tryMe}<Component {...props}/></>
  };

  return WrappedComponent;
};

export default graphiQLCodeBlock(CodeBlock);

Screenshot 2020-09-25 at 17 44 35

Related PRs

@edno edno requested a review from yangshun as a code owner September 25, 2020 15:48
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Sep 25, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Sep 25, 2020

Deploy preview for docusaurus-2 ready!

Built without sensitive environment variables with commit 4a21e1f

https://deploy-preview-3485--docusaurus-2.netlify.app

Copy link
Contributor

@lex111 lex111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a separate function for this purpose to avoid code duplicating?

@edno
Copy link
Contributor Author

edno commented Sep 26, 2020

Let's create a separate function for this purpose to avoid code duplicating?

@lex111 - I've put the full iterator logic into a separate function.

@edno edno requested a review from lex111 September 26, 2020 04:52
@slorber
Copy link
Collaborator

slorber commented Sep 28, 2020

thanks, LGTM

@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Sep 28, 2020
@slorber slorber merged commit 06cc8fb into facebook:master Sep 28, 2020
@edno edno deleted the refactor/init-theme-user branch September 28, 2020 16:02
slorber pushed a commit that referenced this pull request Sep 29, 2020
* Add @theme-init components to user theme

* Refactor loadThemeAlias duplicate code to function
@lex111 lex111 added this to the v2.0.0-alpha.65 milestone Sep 29, 2020
@slorber
Copy link
Collaborator

slorber commented Sep 30, 2020

Hi @edno

I've tried to write some doc for these aliases here #3502, I'd appreciate a review to see if the doc make sense for you.

https://deploy-preview-3502--docusaurus-2.netlify.app/classic/docs/using-themes#wrapping-theme-components

@edno
Copy link
Contributor Author

edno commented Sep 30, 2020

Hi @slorber

The documentation looks good to me. I even learn something new with @theme-original. 😁

@slorber
Copy link
Collaborator

slorber commented Oct 1, 2020

thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants