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

Global import for use on MDX files #3329

Open
yansusanto opened this issue Sep 30, 2024 · 8 comments
Open

Global import for use on MDX files #3329

yansusanto opened this issue Sep 30, 2024 · 8 comments
Labels
documentation Improvements or additions to documentation

Comments

@yansusanto
Copy link

yansusanto commented Sep 30, 2024

import {data, calc} from "@/components/data";

Is it possible to import the above component globally for me to use {data, calc} across all .mdx files? Currently, I'm importing it at all .mdx files.

Any help is much appreciated.

@dimaMachina
Copy link
Collaborator

provide them via this option https://nextra.site/docs/docs-theme/theme-configuration#mdx-components and you will not need to import your components on every mdx file

@yansusanto
Copy link
Author

That's what I did but to no avail,

import React from "react";

import {data, calc} from "@/components/data";

export default {
	main: ({children}) => {
		return (
			{children}
		);
	},
	components: {
		data,
		calc,
		Bleed,
		Cards,
		Callout,
		Steps,
		Tabs,
	},
};

Am I missing something here?

@dimaMachina
Copy link
Collaborator

Does data and calc are react component?

@yansusanto
Copy link
Author

Yes, it is

export const data = {
	italy: {
		population: 1000,
	},
	america: {
		population: 2000,
	},
};

const parseNumber = (value) => {
	if (typeof value === "string") {
		return parseFloat(value.replace(/,/g, ""));
	}
	return value;
};

export const calc = (value, percentage, period = "Y") => {
	const parsedValue = parseNumber(value);
	let result = (parsedValue * percentage) / 100;

	return result.toLocaleString(undefined, {
		minimumFractionDigits: 0,
		maximumFractionDigits: 0,
	});
};

@dimaMachina
Copy link
Collaborator

They are not react components but object and function

@yansusanto
Copy link
Author

You are right, it is not. I guess I have to just import it into every mdx file.

@dimaMachina
Copy link
Collaborator

You can also try to assign them to globalThis in _app file and after they will be globally accessible

@yansusanto
Copy link
Author

Thank you for the pointer, @dimaMachina. You are the best!

import Inter from "@/components/font";
import "@/styles/app.scss";
import {data, calc} from "@/components/data";

export default function App({Component, pageProps}) {
	globalThis.data = data;
	globalThis.calc = calc;

	return (
		<Inter>
			<Component {...pageProps} />
		</Inter>
	);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants