Skip to content

Commit

Permalink
Add builtin animations and namespaced module
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Jul 11, 2023
1 parent 5e4a8a2 commit b998384
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/blog/src/components/BaseHead.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Import the global.css file here so that it is included on
// all pages through the use of the <BaseHead /> component.
import '../styles/global.css';
import { ViewTransitions } from 'astro/components';
import { ViewTransitions } from 'astro:transitions';
export interface Props {
title: string;
Expand Down
1 change: 1 addition & 0 deletions examples/blog/src/components/Header.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import { slide } from 'astro:transitions';
import HeaderLink from './HeaderLink.astro';
import { SITE_TITLE } from '../consts';
---
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/client-base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ declare module 'astro:assets' {
export const { getImage, getConfiguredImageService, Image }: AstroAssets;
}

declare module 'astro:transitions' {
type TransitionModule = typeof import('./dist/transitions/index.js');
export const slide: TransitionModule['slide'];
export const fade: TransitionModule['fade'];

type ViewTransitionsModule = typeof import('./components/ViewTransitions.astro');
export const ViewTransitions: ViewTransitionsModule['default'];
}

type MD = import('./dist/@types/astro').MarkdownInstance<Record<string, any>>;
interface ExportedMarkdownModuleEntities {
frontmatter: MD['frontmatter'];
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"./middleware": {
"types": "./dist/core/middleware/index.d.ts",
"default": "./dist/core/middleware/index.js"
}
},
"./transitions": "./dist/transitions/index.js"
},
"imports": {
"#astro/*": "./dist/*.js"
Expand Down
21 changes: 20 additions & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ export interface AstroBuiltinProps {
'client:only'?: boolean | string;
}

export interface TransitionAnimation {
name: string; // The name of the keyframe
delay?: number | string;
duration?: number | string;
easing?: string;
fillMode?: string;
direction?: string;
}

export interface TransitionAnimationPair {
old: TransitionAnimation | TransitionAnimation[];
new: TransitionAnimation | TransitionAnimation[];
}

export interface TransitionDirectionalAnimations {
forwards: TransitionAnimationPair;
backwards: TransitionAnimationPair;
}

// Allow users to extend this for astro-jsx.d.ts
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AstroClientDirectives {}
Expand All @@ -69,7 +88,7 @@ export interface AstroBuiltinAttributes {
'set:html'?: any;
'set:text'?: any;
'is:raw'?: boolean;
'transition:animate'?: 'morph' | 'slide' | 'fade';
'transition:animate'?: 'morph' | 'slide' | 'fade' | TransitionDirectionalAnimations;
'transition:name'?: string;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import astroScannerPlugin from '../vite-plugin-scanner/index.js';
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js';
import { vitePluginSSRManifest } from '../vite-plugin-ssr-manifest/index.js';
import astroTransitions from '../transitions/vite-plugin-transitions.js';
import { joinPaths } from './path.js';

interface CreateViteOptions {
Expand Down Expand Up @@ -132,6 +133,7 @@ export async function createVite(
astroContentAssetPropagationPlugin({ mode, settings }),
vitePluginSSRManifest(),
settings.config.experimental.assets ? [astroAssetsPlugin({ settings, logging, mode })] : [],
astroTransitions(),
],
publicDir: fileURLToPath(settings.config.publicDir),
root: fileURLToPath(settings.config.root),
Expand Down
65 changes: 65 additions & 0 deletions packages/astro/src/transitions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { TransitionDirectionalAnimations, TransitionAnimationPair } from '../@types/astro';

export function slide({
duration
}: {
duration?: string | number;
} = {}): TransitionDirectionalAnimations {
return {
forwards: {
old: [{
name: 'astroFadeOut',
duration: duration ?? '90ms',
easing: 'cubic-bezier(0.4, 0, 1, 1)',
fillMode: 'both'
}, {
name: 'astroSlideToLeft',
duration: duration ?? '300ms',
easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
fillMode: 'both'
}],
new: [{
name: 'astroFadeIn',
duration: duration ?? '210ms',
easing: 'cubic-bezier(0, 0, 0.2, 1)',
delay: '90ms',
fillMode: 'both'
}, {
name: 'astroSlideFromRight',
duration: duration ?? '300ms',
easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
fillMode: 'both'
}]
},
backwards: {
old: [{ name: 'astroFadeOut' }, { name: 'astroSlideToRight' }],
new: [{ name: 'astroFadeIn' }, { name: 'astroSlideFromLeft' }]
}
};
}

export function fade({
duration
}: {
duration?: string | number;
}): TransitionDirectionalAnimations {
const anim = {
old: {
name: 'astroFadeInOut',
duration: duration ?? '0.2s',
easing: 'linear',
direction: 'forwards',
},
new: {
name: 'astroFadeInOut',
duration: duration ?? '0.3s',
easing: 'linear',
direction: 'backwards',
}
} satisfies TransitionAnimationPair;

return {
forwards: anim,
backwards: anim,
};
}
25 changes: 25 additions & 0 deletions packages/astro/src/transitions/vite-plugin-transitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as vite from 'vite';

const virtualModuleId = 'astro:transitions';

// The virtual module for the astro:transitions namespace
export default function astroTransitions(): vite.Plugin {
const resolvedVirtualModuleId = '\0' + virtualModuleId;

return {
name: 'astro:transitions',
async resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
}
},
load(id) {
if (id === resolvedVirtualModuleId) {
return `
export * from "astro/transitions";
export { default as ViewTransitions } from "astro/components/ViewTransitions.astro";
`;
}
},
};
}
3 changes: 2 additions & 1 deletion packages/astro/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declarationDir": "./dist",
"module": "ES2022",
"outDir": "./dist",
"target": "ES2021"
"target": "ES2021",
"jsx": "preserve"
}
}

0 comments on commit b998384

Please sign in to comment.