-
Notifications
You must be signed in to change notification settings - Fork 2.3k
docs: document creating packages with framework bindings #9222
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
28dddcb
(docs): Document creating packages with Next.js bindings.
anthonyshew c5f85fe
WIP
anthonyshew 083c860
WIP
anthonyshew 92a3b0a
WIP
anthonyshew e9a86b4
WIP
anthonyshew 8c1eef9
WIP
anthonyshew 5e970fd
Merge branch 'main' into shew-f3f9b
anthonyshew 1abcdcc
Update docs/repo-docs/guides/frameworks/framework-bindings.mdx
anthonyshew 0c58aeb
Update docs/repo-docs/guides/frameworks/framework-bindings.mdx
anthonyshew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| --- | ||
| title: Framework bindings in libraries | ||
| description: Learn how to create framework bindings in packages. | ||
| --- | ||
|
|
||
| import { PackageManagerTabs, Tab } from '#/components/tabs'; | ||
| import { Callout } from '#/components/callout'; | ||
|
|
||
| Framework bindings in a [Library Package](/repo/docs/core-concepts/package-types#library-packages) integrate your library's code more deeply with a framework by leveraging APIs from the framework directly in the library. | ||
|
|
||
| To do this, use the `peerDependencies` field in `package.json` of the library, which makes the framework APIs available in your library without installing it directly in the package. | ||
|
|
||
| <Callout type="good-to-know"> | ||
| On this page, we'll be using Next.js for examples, but the concepts below | ||
| apply to any framework or other dependency. | ||
| </Callout> | ||
|
|
||
| ## Example | ||
|
|
||
| Add a `peerDependency` to your library for the dependency that you intend to create bindings for. | ||
|
|
||
| ```json title="./packages/ui/package.json" | ||
| { | ||
| "name": "@repo/ui", | ||
| "peerDependencies": { | ||
| "next": "*" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| <Callout type="good-to-know"> | ||
| In the example above, the `peerDependency` for `next` accepts any version. You | ||
| may want to specify a range (for example, `">=15"`) according to your needs. | ||
| </Callout> | ||
|
|
||
| This will make the dependency available in your library, allowing you to write code like below. Note the `className` prop, which sets a default styling for this component in the monorepo and can be overridden in the `props` object. | ||
anthonyshew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```tsx title="./packages/ui/src/link.tsx" | ||
| import Link from 'next/link'; | ||
| import type { ComponentProps } from 'react'; | ||
|
|
||
| type CustomLinkProps = ComponentProps<typeof Link>; | ||
|
|
||
| export function CustomLink({ children, ...props }: CustomLinkProps) { | ||
| return ( | ||
| <Link className="text-underline hover:text-green-400" {...props}> | ||
| {children} | ||
| </Link> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| The version of `next` that will be resolved for the package will come from the consumers of the library. For example, if Next.js 15 is installed in your applications, the TypeScript types and APIs for `next` will also be Next.js 15. | ||
|
|
||
| ## Splitting framework bindings using entrypoints | ||
|
|
||
| Using export paths to split a package into framework-specific entrypoints is the simplest way to add bindings to a library that aims to support multiple frameworks. By splitting entrypoints, bundlers have an easier time understanding the framework you intend to target and you're less likely to see strange bundling errors. | ||
|
|
||
| The example below shows a library with two entrypoints, each for a different type of link component. These abstractions likely contain your own styles, APIs, and other adjustments on top of the element they're wrapping. | ||
|
|
||
| - `./link`: An `<a>` HTML tag with some default styles from your design system | ||
| - `./next-js/link`: A customized version of [the Next.js `Link` component](https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#link-component) with props that are preset to your organization's preferences | ||
|
Comment on lines
+63
to
+64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would sink in more for me if we added another framework:
anthonyshew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```json title="./packages/ui/package.json" | ||
| { | ||
| "exports": { | ||
| "./link": "./dist/link.js", | ||
| "./next-js/link": "./dist/next-js/link.js" | ||
| }, | ||
| "peerDependencies": { | ||
| "next": "*" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| <Callout type="good-to-know"> | ||
| In the example above, the `peerDependency` for `next` accepts any version. You | ||
| may want to specify a range (for example, `">=15"`) according to your needs. | ||
| </Callout> | ||
|
|
||
| This concept can be applied to any number of frameworks or other dependencies that you'd like to provide bindings for. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| "pages": ["nextjs", "sveltekit", "vite", "nuxt"] | ||
| "pages": ["nextjs", "sveltekit", "vite", "nuxt", "framework-bindings"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.