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

Convert active-class-name example to TypeScript #37676

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { useState, useEffect } from 'react'
import { useRouter } from 'next/router'
import PropTypes from 'prop-types'
import Link from 'next/link'
import React, { Children } from 'react'
import Link, { LinkProps } from 'next/link'
import React, { useState, useEffect, ReactElement, Children } from 'react'

const ActiveLink = ({ children, activeClassName, ...props }) => {
type ActiveLinkProps = LinkProps & {
children: ReactElement
activeClassName: string
}

const ActiveLink = ({
children,
activeClassName,
...props
}: ActiveLinkProps) => {
const { asPath, isReady } = useRouter()

const child = Children.only(children)
Expand All @@ -16,8 +23,10 @@ const ActiveLink = ({ children, activeClassName, ...props }) => {
if (isReady) {
// Dynamic route will be matched via props.as
// Static route will be matched via props.href
const linkPathname = new URL(props.as || props.href, location.href)
.pathname
const linkPathname = new URL(
(props.as || props.href) as string,
location.href
).pathname

// Using URL().pathname to get rid of query and hash
const activePathname = new URL(asPath, location.href).pathname
Expand Down Expand Up @@ -51,8 +60,4 @@ const ActiveLink = ({ children, activeClassName, ...props }) => {
)
}

ActiveLink.propTypes = {
activeClassName: PropTypes.string.isRequired,
}

export default ActiveLink
5 changes: 5 additions & 0 deletions examples/active-class-name/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
11 changes: 8 additions & 3 deletions examples/active-class-name/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"prop-types": "^15.7.2"
"react": "18.1.0",
"react-dom": "18.1.0"
},
"devDependencies": {
"@types/node": "17.0.42",
"@types/react": "18.0.12",
"@types/react-dom": "18.0.5",
"typescript": "4.7.3"
}
}
20 changes: 20 additions & 0 deletions examples/active-class-name/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}