LiveViewNativePlatform

Hex.pm

Installation

To use LiveView Native Platform, add it to your list of dependencies in mix.exs:

def deps do
  [{:live_view_native_platform, "~> 0.1"}]
end

About

LiveView Native Platform is the core Elixir dependency for LiveView Native client platforms. It provides protocols, macros and configuration functions that allow repositories like liveview-client-swiftui to be used as a target of LiveView Native applications. This repository is only intended for client platform developers looking to add support for LiveView Native to their own native code; you should not need to pull this dependency into a LiveView Native application directly.

Usage

To add support for LiveView Native, a client platform must implement the LiveViewNativePlatform protocol. Here’s a barebones example of a module that implements this protocol:

# lib/live_view_native_example_platform/platform.ex
defmodule LiveViewNativeExamplePlatform.Platform do
  defimpl LiveViewNativePlatform.Kit do
    def compile(struct) do
      LiveViewNativePlatform.Env.define(:my_platform, # The unique `platform_id`
        custom_modifiers: struct.custom_modifiers, # Can be omitted if custom modifiers should not be supported
        render_macro: :sigil_MYP, # Optional, if blank templates can only be rendered using `~LVN` sigil with `platform_id` modifier
        tag_handler: LiveViewNativeExamplePlatform.TagEngine, # Optional, defaults to `LiveViewNative.TagEngine`
        template_extension: ".myp.heex", # Optional, defaults to ".#{platform_id}.heex"
        otp_app: :live_view_native_example_platform # The OTP app name of your platform library
      )
    end
  end
end

Here LiveViewNativePlatform.Env.define/3 takes a unique atom to use for identifying the platform and an option list of parameters. These parameters can be used to change certain aspects of the platform. In addition to this protocol, LiveView Native platform libraries must inherit the LiveViewNativePlatform.Kit macro on the top-level module of the library:

defmodule LiveViewNativeExamplePlatform do
  use LiveViewNativePlatform
end

LiveView Native platform libraries will likely also require platform-specific code to run on the client and provide “native” compatibility for the Phoenix LiveView protocol. That code can coexist with the Elixir library (example here) or be provided by a separate package. For the non-Elixir requirements needed to support LiveView Native within a native client, you might consider using liveview-native-core as a dependency within your library.

Learn more