[@types/next-server] add next/amp support (#35132)

This commit is contained in:
Resi Respati 2019-05-01 12:12:22 +07:00 committed by Wesley Wigham
parent 220663aae1
commit 31294de36c
5 changed files with 39 additions and 0 deletions

12
types/next-server/amp.d.ts vendored Normal file
View File

@ -0,0 +1,12 @@
import { NextComponentType } from "next";
export interface WithAmpConfig {
hybrid: boolean;
}
export function withAmp(
Component: NextComponentType | React.ComponentType,
config?: WithAmpConfig
): React.ComponentType;
export function useAmp(): boolean;

View File

@ -0,0 +1,23 @@
import * as React from "react";
import { withAmp, useAmp } from "next-server/amp";
function HomePage() {
return <p>Welcome to AMP + Next.js.</p>;
}
// 1. `withAmp()`
export const WithAmp = withAmp(HomePage);
// 2. `withAmp()` + config
export const WithHybridAmp = withAmp(HomePage, { hybrid: true });
// 3. `useAmp()` hook
export function Image({ src, width, height }: any) {
const isAmp = useAmp();
// The 'amp-img' element is not in the JSX namespace, so for now we
// use `React.createElement()` to construct the amp-img element.
return isAmp ? React.createElement('amp-img', { src, width, height }) : (
<img src={src} width={width} height={height} />
);
}

View File

@ -20,6 +20,7 @@
},
"files": [
"index.d.ts",
"amp.d.ts",
"asset.d.ts",
"config.d.ts",
"constants.d.ts",
@ -29,6 +30,7 @@
"next-config.d.ts",
"router.d.ts",
"test/next-server-tests.ts",
"test/next-server-amp-tests.tsx",
"test/next-server-constants-tests.ts",
"test/next-server-head-tests.tsx",
"test/next-server-link-tests.tsx",

1
types/next/amp.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from 'next-server/amp';

View File

@ -21,6 +21,7 @@
"files": [
"index.d.ts",
"constants.d.ts",
"amp.d.ts",
"app.d.ts",
"document.d.ts",
"dynamic.d.ts",