Skip to content

Commit

Permalink
refactor: 💡 theming
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Feb 12, 2024
1 parent 0687c9b commit 02233cb
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 187 deletions.
9 changes: 1 addition & 8 deletions apps/dotlottie-playground/src/components/dropzone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ReactDropzone, { type DropzoneState, type FileError, type FileRejection,
import { toast } from 'react-toastify';

interface DropzoneProps {
accept: 'lottie' | 'json' | 'lss';
accept: 'lottie' | 'json';
children: (state: DropzoneState) => JSX.Element;
multiple?: boolean;
noClick?: boolean;
Expand Down Expand Up @@ -40,13 +40,6 @@ export const Dropzone: React.FC<DropzoneProps> = ({ accept, children, multiple,
};
}

if (accept === 'lss' && !fileName.endsWith('.lss')) {
return {
message: 'Invalid .lss',
code: ErrorCode.FileInvalidType,
};
}

return null;
}, []);

Expand Down
11 changes: 6 additions & 5 deletions apps/dotlottie-playground/src/components/file-tree/file-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/

import React from 'react';
import { BsFiletypeJson, BsFiletypeCss } from 'react-icons/bs';
import { BsFiletypeJson } from 'react-icons/bs';

import type { SupportedFileTypes } from '.';

export const FileIcon = ({ type }: { type: SupportedFileTypes }): JSX.Element => {
if (type === 'lss') {
return <BsFiletypeCss />;
} else {
return <BsFiletypeJson />;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (type !== 'json') {
return <></>;
}

return <BsFiletypeJson />;
};
14 changes: 6 additions & 8 deletions apps/dotlottie-playground/src/components/file-tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2023 Design Barn Inc.
*/

import React, { useCallback, useMemo, useState } from 'react';
import React, { useCallback, useState } from 'react';
import { useKey } from 'react-use';

import { useDotLottie } from '../../hooks/use-dotlottie';
Expand All @@ -14,7 +14,7 @@ import { AddNew } from './add-new';
import { EditableItem } from './editable-item';
import { Title } from './title';

const FILE_TYPES = ['json', 'lss'] as const;
const FILE_TYPES = ['json'] as const;

export type SupportedFileTypes = typeof FILE_TYPES[number];

Expand Down Expand Up @@ -54,8 +54,6 @@ export const FileTree: React.FC<FileTreeProps> = ({
[onClick, title],
);

const fileExtention = useMemo(() => (title.toLowerCase() === 'themes' ? 'lss' : 'json'), [title]);

const handleRemove = useCallback(
(fileName: string) => {
onRemove?.(title, fileName);
Expand All @@ -79,10 +77,10 @@ export const FileTree: React.FC<FileTreeProps> = ({

const handleAddNew = useCallback(
(value: string) => {
onAddNew?.(title, `${value}.${fileExtention}`);
onAddNew?.(title, `${value}.json`);
setDisplayAdd(false);
},
[onAddNew, title, fileExtention],
[onAddNew, title],
);

const handleRename = useCallback(
Expand Down Expand Up @@ -121,7 +119,7 @@ export const FileTree: React.FC<FileTreeProps> = ({
buttons={title === 'Animations' ? ['upload'] : ['upload', 'add']}
/>
<div className="relative h-full overflow-y-auto custom-scrollbar">
<Dropzone onDrop={onDrop} accept={title === 'Themes' ? 'lss' : 'json'} noClick>
<Dropzone onDrop={onDrop} accept={'json'} noClick>
{(state): JSX.Element => {
return (
<div {...state.getRootProps()}>
Expand Down Expand Up @@ -157,7 +155,7 @@ export const FileTree: React.FC<FileTreeProps> = ({

{displayAdd && (
<li>
<AddNew onAdd={handleAddNew} extension={title === 'Themes' ? 'lss' : 'json'} />
<AddNew onAdd={handleAddNew} extension={'json'} />
</li>
)}
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const Title: React.FC<TitleProps> = ({ buttons, onClickAdd, onUpload, tit

case 'upload':
buttonsToDisplay.push(
<Dropzone key={item} onDrop={onDrop} accept={title === 'Themes' ? 'lss' : 'json'}>
<Dropzone key={item} onDrop={onDrop} accept={'json'}>
{({ getInputProps, getRootProps }): JSX.Element => (
<button {...getRootProps()} className="hover:text-white" title="Upload">
<input {...getInputProps()} />
Expand Down
12 changes: 5 additions & 7 deletions apps/dotlottie-playground/src/components/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export const Playground: React.FC<PlaygroundProps> = ({ file: dotLottieFile, fil
async (folder: string, fileName: string) => {
dispatch(clearEditorState());
let fileContent: string | undefined;
let isTheme = false;

switch (folder) {
case 'States':
Expand All @@ -180,7 +179,6 @@ export const Playground: React.FC<PlaygroundProps> = ({ file: dotLottieFile, fil

case 'Themes':
fileContent = await dotLottie.getTheme(fileName)?.toString();
isTheme = true;
break;

default:
Expand All @@ -191,9 +189,9 @@ export const Playground: React.FC<PlaygroundProps> = ({ file: dotLottieFile, fil
dispatch(
setEditorFile({
name: fileName,
type: isTheme ? 'css' : 'json',
type: 'json',
path: folder,
content: isTheme ? fileContent : formatJSON(fileContent),
content: formatJSON(fileContent),
}),
);
}
Expand Down Expand Up @@ -243,7 +241,7 @@ export const Playground: React.FC<PlaygroundProps> = ({ file: dotLottieFile, fil

const handleUpload = useCallback(
async (title: string, file: File) => {
const fileName = processFilename(file.name).replace(/(.json|.lss)/gu, '');
const fileName = processFilename(file.name).replace(/(.json)/gu, '');
let parsedContent: unknown;

switch (title) {
Expand Down Expand Up @@ -276,7 +274,7 @@ export const Playground: React.FC<PlaygroundProps> = ({ file: dotLottieFile, fil

const handleAddNew = useCallback(
async (title: string, fileName: string) => {
const updatedFileName = processFilename(fileName).replace(/(.json|.lss)/gu, '');
const updatedFileName = processFilename(fileName).replace(/(.json)/gu, '');
let mockState: DotLottieStateMachine;

switch (title) {
Expand All @@ -287,7 +285,7 @@ export const Playground: React.FC<PlaygroundProps> = ({ file: dotLottieFile, fil
break;

case 'Themes':
addDotLottieTheme('/* Make your animations colorful */', updatedFileName);
addDotLottieTheme('{}', updatedFileName);
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion apps/dotlottie-playground/src/hooks/use-dotlottie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const DotLottieProvider: React.FC<{ children: ReactNode }> = ({ children
.map((item) => {
return {
name: `${item.id}`,
type: 'lss',
type: 'json',
};
})
.sort((item1, item2) => (item1.name > item2.name ? 1 : -1));
Expand Down
2 changes: 1 addition & 1 deletion apps/dotlottie-playground/src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2023 Design Barn Inc.
*/

export const SUPPORTED_FILE_TYPES = ['json', 'lss'] as const;
export const SUPPORTED_FILE_TYPES = ['json'] as const;
export const EDITOR_ACTIONS = [
'themes_add',
'themes_remove',
Expand Down
4 changes: 1 addition & 3 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
"type-check": "tsc --noEmit"
},
"dependencies": {
"@dotlottie/dotlottie-js": "^0.6.2",
"@lottiefiles/relottie": "1.0.0",
"@lottiefiles/relottie-style": "0.4.3",
"@dotlottie/dotlottie-js": "^0.7.0",
"@preact/signals-core": "^1.2.3",
"howler": "^2.2.3",
"lottie-web": "^5.12.2",
Expand Down
8 changes: 4 additions & 4 deletions packages/common/src/dotlottie-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DotLottieLoader {

private readonly _animationsMap: Map<string, AnimationData> = new Map();

private readonly _themeMap: Map<string, string> = new Map();
private readonly _themeMap: Map<string, Record<string, unknown>> = new Map();

private readonly _stateMachinesMap: Map<string, LottieStateMachine> = new Map();

Expand All @@ -34,7 +34,7 @@ export class DotLottieLoader {
return this._animationsMap;
}

public get themeMap(): Map<string, string> {
public get themeMap(): Map<string, Record<string, unknown>> {
return this._themeMap;
}

Expand Down Expand Up @@ -143,9 +143,9 @@ export class DotLottieLoader {
return animation;
}

public async getTheme(themeId: string): Promise<string | undefined> {
public async getTheme(themeId: string): Promise<Record<string, unknown> | undefined> {
if (this._themeMap.get(themeId)) {
return this._themeMap.get(themeId) as string;
return this._themeMap.get(themeId);
}

if (!this._dotLottie) {
Expand Down
13 changes: 8 additions & 5 deletions packages/common/src/dotlottie-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import pkg from '../package.json';
import type { DotLottieAudio } from './dotlottie-audio';
import { DotLottieLoader } from './dotlottie-loader';
import { loadStateMachines } from './dotlottie-state-machine-loader';
import { applyLottieStyleSheet } from './dotlottie-styler';
import type { DotLottieStateMachineManager } from './state/dotlottie-state-machine-manager';
import { Store } from './store';
import {
Expand Down Expand Up @@ -528,7 +527,7 @@ export class DotLottieCommonPlayer {
return this._dotLottieLoader.animationsMap;
}

public get themes(): Map<string, string> {
public get themes(): Map<string, Record<string, unknown>> {
return this._dotLottieLoader.themeMap;
}

Expand Down Expand Up @@ -1774,14 +1773,18 @@ export class DotLottieCommonPlayer {
};

// load the dependencies in parallel
const [lottieStyleSheet, lottiePlayer, audioFactory] = await Promise.all([
const [theme, lottiePlayer, audioFactory] = await Promise.all([
this._dotLottieLoader.getTheme(defaultTheme),
this._getLottiePlayerInstance(),
this._getAudioFactory(),
]);

if (lottieStyleSheet && this._animation) {
this._animation = await applyLottieStyleSheet(this._animation, lottieStyleSheet);
if (theme && this._animation) {
this._animation = {
...this._animation,
// @ts-ignore

Check warning on line 1785 in packages/common/src/dotlottie-player.ts

View workflow job for this annotation

GitHub Actions / validate

Do not use "@ts-ignore" because it alters compilation errors
slots: theme,
};
} else {
this._animation = await this._dotLottieLoader.getAnimation(this._currentAnimationId ?? '');
}
Expand Down
24 changes: 0 additions & 24 deletions packages/common/src/dotlottie-styler.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/react-player/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The `DotLottiePlayer` component accepts the following props:
| `renderer` | How to render | `'svg' \| 'html' \| 'canvas'` | `svg` |
| `speed` | Play speed | `number` | `1` |
| `onEvent` | Listen to player events | `function` | `undefined` |
| `defaultTheme` | Default lss theme to use | `string` | `undefined` |
| `defaultTheme` | Default .lottie theme to use | `string` | `undefined` |

## Events

Expand Down
Loading

0 comments on commit 02233cb

Please sign in to comment.