# XPost — SvelteKit UI

Package: sveltekit-ui 1.1.94

Page: https://www.sveltekit-ui.com/components/x_post

Category: Media

Embeds an X (formerly Twitter) post given its post ID, handling script loading and rendering. Supports internal and external links with optional previews and QR codes.

Public component import: `XPost`

Public manager import: `create_x_post_manager`

## Usage

```svelte
<script>
  // Import the shared stylesheet once in your application's root layout.
  import "sveltekit-ui/style.css"
  import { XPost, create_x_post_manager } from "sveltekit-ui"

  // The component loads X's widget script to embed this public post.

  const manager = create_x_post_manager({
    post_id: "20",
    is_dark_theme: false,
  })
</script>

<XPost {manager} />
```

## Configuration

### `id`

- type: string or null
- default: generated unique ID
- description: A unique string ID for this XPost component. If not provided, a unique ID is generated automatically.

### `post_id`

- type: string
- default: null
- description: The unique identifier of the X (Twitter) post to embed. This is required for rendering the post.

### `href`

- type: string or null
- default: null
- description: The URL associated with the post. If it starts with '/', it's treated as an internal link; otherwise, as an external link.

### `display_text`

- type: string or null
- default: null
- description: The text to display for the link. If not provided, child content can be used instead.

### `text_color`

- type: CSS color or null
- default: null
- description: The color of the link text. If not specified, it inherits the default styling.

### `is_show_preview`

- type: boolean
- default: !is_internal_link
- description: Determines whether to show a popover preview of the post when clicked. Defaults to true for external links and false for internal links.

### `is_dark_theme`

- type: boolean
- default: true
- description: If true, the embedded post uses a dark theme. Otherwise, a light theme is applied.

### `height`

- type: number or null
- default: min(40rem, 50vh)
- description: The height of the popover containing the embedded post, specified in rem. Defaults to 'min(40rem, 50vh)' if not set.

### `width`

- type: number or null
- default: clamp(60rem, 80vw, 100%)
- description: The width of the popover containing the embedded post, specified in rem. Defaults to 'clamp(60rem, 80vw, 100%)' if not set.

### `on_goto_external_link`

- type: function or null
- default: null
- description: Optional callback function that is invoked when an external link is opened. Receives the `href` as an argument.

## Methods

### `load_script()`

- description: Dynamically loads the Twitter widgets script necessary for embedding the post. Once loaded, it triggers the rendering of the embedded post.

### `render_x_post()`

- description: Embeds the X (Twitter) post into the designated container using `twttr.widgets.createTweet`. Handles theme settings and error messaging if the post fails to load.

### `handle_a_click()`

- description: Handles click events on the link. Prevents the default behavior and determines whether to navigate internally, show a preview, or open the link externally based on configuration.

### `open_link()`

- description: Opens the link associated with the post. If it's an internal link, it uses SvelteKit's `goto` for navigation. For external links, it opens the URL in a new browser tab and invokes the `on_goto_external_link` callback if provided.

### `set_val()`

- description: Updates the `val` state with a new value and re-initializes the embedded post if necessary.

## Returned state

### `post_id`

- type: string
- description: The unique identifier of the X (Twitter) post being embedded.

### `is_dark_theme`

- type: boolean
- description: Indicates whether the embedded post is using a dark theme.

### `theme`

- type: string
- description: The current theme of the embedded post ('dark' or 'light').

### `popover_manager`

- type: object
- description: Popover manager for controlling the display of the post preview. Methods include `open()` and `close()`.

### `toggle_popover_manager`

- type: object
- description: Button manager for toggling the popover. Manages the state and behavior of the toggle button.

### `height`

- type: string
- description: The height of the popover container, in rem.

### `width`

- type: string
- description: The width of the popover container, in rem.
