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

fix: render image loading state #271

Merged
merged 1 commit into from
Apr 2, 2024
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
5 changes: 5 additions & 0 deletions .changeset/rich-needles-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/render": patch
---

fix: image loading state
22 changes: 17 additions & 5 deletions packages/render/src/frame-ui.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FrameTheme, FrameState } from "./types.js";
import React, { ImgHTMLAttributes } from "react";
import React, { ImgHTMLAttributes, useEffect } from "react";
import { FrameButton } from "frames.js";

export const defaultTheme: Required<FrameTheme> = {
Expand All @@ -26,6 +26,14 @@ export type FrameUIProps = {

/** A UI component only, that should be easy for any app to integrate */
export function FrameUI({ frameState, theme, FrameImage }: FrameUIProps) {
const [isImageLoading, setIsImageLoading] = React.useState(true);

const isLoading = !!frameState.isLoading || isImageLoading;

useEffect(() => {
if (frameState.frame?.image) setIsImageLoading(true);
}, [frameState]);

const resolvedTheme = getThemeWithDefaults(theme ?? {});
if (!frameState.homeframeUrl) return <div>Missing frame url</div>;
if (frameState.error) {
Expand All @@ -47,7 +55,7 @@ export function FrameUI({ frameState, theme, FrameImage }: FrameUIProps) {
alt="Frame image"
width={"100%"}
style={{
filter: frameState.isLoading ? "blur(4px)" : undefined,
filter: isLoading ? "blur(4px)" : undefined,
borderTopLeftRadius: `${resolvedTheme.buttonRadius}px`,
borderTopRightRadius: `${resolvedTheme.buttonRadius}px`,
border: `1px solid ${resolvedTheme.buttonBorderColor}`,
Expand All @@ -58,6 +66,10 @@ export function FrameUI({ frameState, theme, FrameImage }: FrameUIProps) {
? "1/1"
: "1.91/1",
}}
onLoad={() => {
setIsImageLoading(false);
}}
onError={() => setIsImageLoading(false)}
/>
{frameState.frame.inputText && (
<input
Expand All @@ -84,17 +96,17 @@ export function FrameUI({ frameState, theme, FrameImage }: FrameUIProps) {
(frameButton: FrameButton, index: number) => (
<button
type="button"
disabled={!!frameState.isLoading}
disabled={isLoading}
className={`p-2 ${
frameState.isLoading ? "bg-gray-100" : ""
isLoading ? "bg-gray-100" : ""
} border text-sm text-gray-800 rounded`}
style={{
flex: "1 1 0px",
// fixme: hover style
backgroundColor: resolvedTheme.buttonBg,
borderColor: resolvedTheme.buttonBorderColor,
color: resolvedTheme.buttonColor,
cursor: frameState.isLoading ? undefined : "pointer",
cursor: isLoading ? undefined : "pointer",
}}
onClick={() => frameState.onButtonPress(frameButton, index)}
key={index}
Expand Down
Loading